From e7b51324f2578cc9c1ef4eeababfcec14162a3e9 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 21 Mar 2017 14:17:34 -0700 Subject: adding cache-control age and disabler --- system/admin/config/config.go | 15 +++++++++++++++ system/db/cache.go | 32 +++++++++++++++++++++----------- system/db/config.go | 5 +++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/system/admin/config/config.go b/system/admin/config/config.go index d623592..015c081 100644 --- a/system/admin/config/config.go +++ b/system/admin/config/config.go @@ -18,6 +18,8 @@ type Config struct { Etag string `json:"etag"` DisableCORS bool `json:"cors_disabled"` DisableGZIP bool `json:"gzip_disabled"` + DisableHTTPCache bool `json:"cache_disabled"` + CacheMaxAge int64 `json:"cache_max_age"` CacheInvalidate []string `json:"cache"` BackupBasicAuthUser string `json:"backup_basic_auth_user"` BackupBasicAuthPassword string `json:"backup_basic_auth_password"` @@ -99,6 +101,19 @@ func (c *Config) MarshalEditor() ([]byte, error) { "true": "Disable GZIP", }), }, + editor.Field{ + View: editor.Checkbox("DisableHTTPCache", c, map[string]string{ + "label": "Disable HTTP Cache (overrides 'Cache-Control' header)", + }, map[string]string{ + "true": "Disable HTTP Cache", + }), + }, + editor.Field{ + View: editor.Input("CacheMaxAge", c, map[string]string{ + "label": "Max-Age value for HTTP caching (in seconds, 0 = 2592000)", + "type": "text", + }), + }, editor.Field{ View: editor.Checkbox("CacheInvalidate", c, map[string]string{ "label": "Invalidate cache on save", 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) + } }) } diff --git a/system/db/config.go b/system/db/config.go index 2706e97..6a409c2 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -13,6 +13,11 @@ import ( "github.com/gorilla/schema" ) +const ( + // DefaultMaxAge provides a 2592000 second (30-day) cache max-age setting + DefaultMaxAge = int64(60 * 60 * 24 * 30) +) + var configCache map[string]interface{} func init() { -- cgit v1.2.3 From 6d78e9f59a6fac341840d23544a045853a07fef4 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 21 Mar 2017 14:48:22 -0700 Subject: JSON decoded val for int variants are float64 - need to cast after assertion --- system/db/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/db/cache.go b/system/db/cache.go index 55dd42a..fbb0fd5 100644 --- a/system/db/cache.go +++ b/system/db/cache.go @@ -16,7 +16,7 @@ func CacheControl(next http.Handler) http.HandlerFunc { res.Header().Add("Cache-Control", "no-cache") next.ServeHTTP(res, req) } else { - age := ConfigCache("cache_max_age").(int64) + age := int64(ConfigCache("cache_max_age").(float64)) etag := ConfigCache("etag").(string) if age == 0 { age = DefaultMaxAge -- cgit v1.2.3