mirror of https://github.com/jlelse/GoBlog
Improve Cache
This commit is contained in:
parent
f1d3fd6ad9
commit
8c73686a6f
13
cache.go
13
cache.go
|
@ -112,6 +112,13 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
|
||||||
}
|
}
|
||||||
if item == nil || item.expired() {
|
if item == nil || item.expired() {
|
||||||
// No cache available
|
// No cache available
|
||||||
|
// Remove problematic headers
|
||||||
|
r.Header.Del("If-Modified-Since")
|
||||||
|
r.Header.Del("If-Unmodified-Since")
|
||||||
|
r.Header.Del("If-None-Match")
|
||||||
|
r.Header.Del("If-Match")
|
||||||
|
r.Header.Del("If-Range")
|
||||||
|
r.Header.Del("Range")
|
||||||
// Record request
|
// Record request
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
next.ServeHTTP(recorder, r)
|
next.ServeHTTP(recorder, r)
|
||||||
|
@ -123,9 +130,13 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
|
||||||
_, _ = io.Copy(h, bytes.NewReader(body))
|
_, _ = io.Copy(h, bytes.NewReader(body))
|
||||||
hash := fmt.Sprintf("%x", h.Sum(nil))
|
hash := fmt.Sprintf("%x", h.Sum(nil))
|
||||||
exp, _ := strconv.Atoi(result.Header.Get(cacheInternalExpirationHeader))
|
exp, _ := strconv.Atoi(result.Header.Get(cacheInternalExpirationHeader))
|
||||||
|
lastMod := time.Now()
|
||||||
|
if lm := result.Header.Get("Last-Modified"); lm != "" {
|
||||||
|
lastMod, _ = dateparse.ParseLocal(lm)
|
||||||
|
}
|
||||||
item = &cacheItem{
|
item = &cacheItem{
|
||||||
expiration: exp,
|
expiration: exp,
|
||||||
creationTime: time.Now(),
|
creationTime: lastMod,
|
||||||
hash: hash,
|
hash: hash,
|
||||||
code: result.StatusCode,
|
code: result.StatusCode,
|
||||||
header: result.Header,
|
header: result.Header,
|
||||||
|
|
Loading…
Reference in New Issue