diff options
Diffstat (limited to 'system/db/cache.go')
-rw-r--r-- | system/db/cache.go | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/system/db/cache.go b/system/db/cache.go index 0120147..55dd42a 100644 --- a/system/db/cache.go +++ b/system/db/cache.go @@ -11,19 +11,29 @@ import ( // CacheControl sets the default cache policy on static asset responses func CacheControl(next http.Handler) http.HandlerFunc { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - etag := ConfigCache("etag").(string) - policy := fmt.Sprintf("max-age=%d, public", 60*60*24*30) - res.Header().Add("ETag", etag) - res.Header().Add("Cache-Control", policy) - - if match := req.Header.Get("If-None-Match"); match != "" { - if strings.Contains(match, etag) { - res.WriteHeader(http.StatusNotModified) - return + cacheDisabled := ConfigCache("cache_disabled").(bool) + if cacheDisabled { + res.Header().Add("Cache-Control", "no-cache") + next.ServeHTTP(res, req) + } else { + age := ConfigCache("cache_max_age").(int64) + etag := ConfigCache("etag").(string) + if age == 0 { + age = DefaultMaxAge + } + policy := fmt.Sprintf("max-age=%d, public", age) + res.Header().Add("ETag", etag) + res.Header().Add("Cache-Control", policy) + + if match := req.Header.Get("If-None-Match"); match != "" { + if strings.Contains(match, etag) { + res.WriteHeader(http.StatusNotModified) + return + } } - } - next.ServeHTTP(res, req) + next.ServeHTTP(res, req) + } }) } |