Improve Cache

This commit is contained in:
Jan-Lukas Else 2021-01-10 14:22:02 +01:00
parent 0fba6ddfeb
commit f1d3fd6ad9
3 changed files with 9 additions and 11 deletions

View File

@ -56,7 +56,7 @@ func cacheMiddleware(next http.Handler) http.Handler {
} }
if ifModifiedSinceHeader := r.Header.Get("If-Modified-Since"); ifModifiedSinceHeader != "" { if ifModifiedSinceHeader := r.Header.Get("If-Modified-Since"); ifModifiedSinceHeader != "" {
t, err := dateparse.ParseAny(ifModifiedSinceHeader) t, err := dateparse.ParseAny(ifModifiedSinceHeader)
if err == nil && t.Unix() >= cache.creationTime { if err == nil && t.After(cache.creationTime) {
// send 304 // send 304
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)
return return
@ -80,12 +80,10 @@ func cacheKey(r *http.Request) string {
func setCacheHeaders(w http.ResponseWriter, cache *cacheItem) { func setCacheHeaders(w http.ResponseWriter, cache *cacheItem) {
w.Header().Del(cacheInternalExpirationHeader) w.Header().Del(cacheInternalExpirationHeader)
w.Header().Set("ETag", cache.hash) w.Header().Set("ETag", cache.hash)
w.Header().Set("Last-Modified", time.Unix(cache.creationTime, 0).UTC().Format(http.TimeFormat)) w.Header().Set("Last-Modified", cache.creationTime.UTC().Format(http.TimeFormat))
if w.Header().Get("Cache-Control") == "" { if w.Header().Get("Cache-Control") == "" {
if cache.expiration != 0 { if cache.expiration != 0 {
expiresIn := cache.creationTime + int64(cache.expiration) - time.Now().Unix() w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", cache.expiration, cache.expiration))
// Set expires time
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", expiresIn, cache.expiration))
} else { } else {
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", appConfig.Cache.Expiration, appConfig.Cache.Expiration/3, appConfig.Cache.Expiration)) w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", appConfig.Cache.Expiration, appConfig.Cache.Expiration/3, appConfig.Cache.Expiration))
} }
@ -94,7 +92,7 @@ func setCacheHeaders(w http.ResponseWriter, cache *cacheItem) {
type cacheItem struct { type cacheItem struct {
expiration int expiration int
creationTime int64 creationTime time.Time
hash string hash string
code int code int
header http.Header header http.Header
@ -103,7 +101,7 @@ type cacheItem struct {
func (c *cacheItem) expired() bool { func (c *cacheItem) expired() bool {
if c.expiration != 0 { if c.expiration != 0 {
return c.creationTime < time.Now().Unix()-int64(c.expiration) return time.Now().After(c.creationTime.Add(time.Duration(c.expiration) * time.Second))
} }
return false return false
} }
@ -127,7 +125,7 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
exp, _ := strconv.Atoi(result.Header.Get(cacheInternalExpirationHeader)) exp, _ := strconv.Atoi(result.Header.Get(cacheInternalExpirationHeader))
item = &cacheItem{ item = &cacheItem{
expiration: exp, expiration: exp,
creationTime: time.Now().Unix(), creationTime: time.Now(),
hash: hash, hash: hash,
code: result.StatusCode, code: result.StatusCode,
header: result.Header, header: result.Header,

2
go.mod
View File

@ -58,7 +58,7 @@ require (
golang.org/x/mod v0.4.0 // indirect golang.org/x/mod v0.4.0 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 // indirect golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/text v0.3.5 // indirect golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e // indirect golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e // indirect

4
go.sum
View File

@ -454,8 +454,8 @@ golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 h1:wHn06sgWHMO1VsQ8F+KzDJx/JzqfsNLnc+oEi07qD7s= golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=