diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 0c00d18d1..6d2128d4b 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -279,9 +279,9 @@ func (s *SouinBaseHandler) Store( } type singleflightValue struct { - body []byte + body []byte headers http.Header - code int + code int } func (s *SouinBaseHandler) Upstream( @@ -294,7 +294,7 @@ func (s *SouinBaseHandler) Upstream( s.Configuration.GetLogger().Sugar().Debug("Request the upstream server") prometheus.Increment(prometheus.RequestCounter) shared := true - + sfValue, err, _ := s.singleflightPool.Do(cachedKey, func() (interface{}, error) { shared = false if e := next(customWriter, rq); e != nil { @@ -323,9 +323,9 @@ func (s *SouinBaseHandler) Upstream( err := s.Store(customWriter, rq, requestCc, cachedKey) return singleflightValue{ - body: customWriter.Buf.Bytes(), + body: customWriter.Buf.Bytes(), headers: customWriter.Headers, - code: customWriter.statusCode, + code: customWriter.statusCode, }, err } }) @@ -349,7 +349,7 @@ func (s *SouinBaseHandler) Revalidate(validator *rfc.Revalidator, next handlerFu prometheus.Increment(prometheus.RequestRevalidationCounter) shared := true - + sfValue, err, _ := s.singleflightPool.Do(cachedKey, func() (interface{}, error) { shared = false err := next(customWriter, rq) @@ -384,9 +384,9 @@ func (s *SouinBaseHandler) Revalidate(validator *rfc.Revalidator, next handlerFu ) return singleflightValue{ - body: customWriter.Buf.Bytes(), + body: customWriter.Buf.Bytes(), headers: customWriter.Headers, - code: customWriter.statusCode, + code: customWriter.statusCode, }, err }) diff --git a/plugins/caddy/httpcache.go b/plugins/caddy/httpcache.go index 74d66af99..6bc396a0c 100644 --- a/plugins/caddy/httpcache.go +++ b/plugins/caddy/httpcache.go @@ -35,6 +35,7 @@ func init() { type SouinCaddyMiddleware struct { *middleware.SouinBaseHandler logger *zap.Logger + cacheKeys configurationtypes.CacheKeys Configuration Configuration // Logger level, fallback on caddy's one when not redefined. LogLevel string `json:"log_level,omitempty"` @@ -86,30 +87,30 @@ func (s *SouinCaddyMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request } func (s *SouinCaddyMiddleware) configurationPropertyMapper() error { - defaultCache := DefaultCache{ - Badger: s.Badger, - Nuts: s.Nuts, - Key: s.Key, - DefaultCacheControl: s.DefaultCacheControl, - CacheName: s.CacheName, - Distributed: s.Olric.URL != "" || s.Olric.Path != "" || s.Olric.Configuration != nil || s.Etcd.Configuration != nil || s.Redis.URL != "" || s.Redis.Configuration != nil, - Headers: s.Headers, - Olric: s.Olric, - Etcd: s.Etcd, - Redis: s.Redis, - Timeout: s.Timeout, - TTL: s.TTL, - Stale: s.Stale, - Storers: s.Storers, - } - // if s.Configuration == nil { - // s.Configuration = Configuration{ - // CacheKeys: s.cacheKeys, - // DefaultCache: defaultCache, - // LogLevel: s.LogLevel, - // } - // } - s.Configuration.DefaultCache = defaultCache + if s.Configuration.GetDefaultCache() == nil { + defaultCache := DefaultCache{ + Badger: s.Badger, + Nuts: s.Nuts, + Key: s.Key, + DefaultCacheControl: s.DefaultCacheControl, + CacheName: s.CacheName, + Distributed: s.Olric.URL != "" || s.Olric.Path != "" || s.Olric.Configuration != nil || s.Etcd.Configuration != nil || s.Redis.URL != "" || s.Redis.Configuration != nil, + Headers: s.Headers, + Olric: s.Olric, + Etcd: s.Etcd, + Redis: s.Redis, + Timeout: s.Timeout, + TTL: s.TTL, + Stale: s.Stale, + Storers: s.Storers, + } + s.Configuration = Configuration{ + CacheKeys: s.cacheKeys, + DefaultCache: defaultCache, + LogLevel: s.LogLevel, + } + } + return nil } @@ -327,6 +328,7 @@ func (s *SouinCaddyMiddleware) UnmarshalCaddyfile(h *caddyfile.Dispenser) error s.Configuration = Configuration{ DefaultCache: dc, } + return parseConfiguration(&s.Configuration, h, false) }