diff --git a/pkg/uhttp/gocache.go b/pkg/uhttp/gocache.go index 7d708479..826467ae 100644 --- a/pkg/uhttp/gocache.go +++ b/pkg/uhttp/gocache.go @@ -16,6 +16,19 @@ import ( "go.uber.org/zap" ) +const ( + cacheTTLMaximum = 31536000 // 31536000 seconds = one year + cacheTTLDefault = 3600 // 3600 seconds = one hour + defaultCacheSize = 50 // MB +) + +type CacheConfig struct { + LogDebug bool + CacheTTL int64 // If 0, cache is disabled + CacheMaxSize int +} +type ContextKey struct{} + type GoCache struct { rootLibrary *bigCache.BigCache } @@ -34,6 +47,10 @@ func (n *NoopCache) Set(req *http.Request, value *http.Response) error { return nil } +func (cc *CacheConfig) ToString() string { + return fmt.Sprintf("CacheTTL: %d, CacheMaxSize: %d, LogDebug: %t", cc.CacheTTL, cc.CacheMaxSize, cc.LogDebug) +} + func DefaultCacheConfig() CacheConfig { return CacheConfig{ CacheTTL: cacheTTLDefault, @@ -94,17 +111,19 @@ func NewHttpCache(ctx context.Context, config *CacheConfig) (icache, error) { disableCache = false } if disableCache { - l.Debug("BATON_DISABLE_HTTP_CACHE set, disabling cache.", zap.Int64("CacheTTL", config.CacheTTL)) + l.Debug("BATON_DISABLE_HTTP_CACHE set, disabling cache.") return noopCache, nil } cacheBackend := os.Getenv("BATON_HTTP_CACHE_BACKEND") if cacheBackend == "" { + l.Debug("defaulting to db-cache") cacheBackend = "db" } switch cacheBackend { case "memory": + l.Debug("Using in-memory cache") memCache, err := NewGoCache(ctx, *config) if err != nil { l.Error("error creating http cache (in-memory)", zap.Error(err)) @@ -112,6 +131,7 @@ func NewHttpCache(ctx context.Context, config *CacheConfig) (icache, error) { } cache = memCache case "db": + l.Debug("Using db cache") dbCache, err := NewDBCache(ctx, *config) if err != nil { l.Error("error creating http cache (db-cache)", zap.Error(err)) diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index dc0440c5..bd0b07fe 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -26,9 +26,6 @@ const ( applicationFormUrlencoded = "application/x-www-form-urlencoded" applicationVndApiJSON = "application/vnd.api+json" acceptHeader = "Accept" - cacheTTLMaximum = 31536000 // 31536000 seconds = one year - cacheTTLDefault = 3600 // 3600 seconds = one hour - defaultCacheSize = 50 // MB ) type WrapperResponse struct { @@ -51,12 +48,6 @@ type ( DoOption func(resp *WrapperResponse) error RequestOption func() (io.ReadWriter, map[string]string, error) - ContextKey struct{} - CacheConfig struct { - LogDebug bool - CacheTTL int64 // If 0, cache is disabled - CacheMaxSize int - } ) func NewBaseHttpClient(httpClient *http.Client) *BaseHttpClient { @@ -192,25 +183,19 @@ func WrapErrorsWithRateLimitInfo(preferredCode codes.Code, resp *http.Response, func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Response, error) { var ( - cacheKey string - err error - resp *http.Response + err error + resp *http.Response ) l := ctxzap.Extract(req.Context()) if req.Method == http.MethodGet { - cacheKey, err = CreateCacheKey(req) - if err != nil { - return nil, err - } - resp, err = c.baseHttpCache.Get(req) if err != nil { return nil, err } if resp == nil { - l.Debug("http cache miss", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String())) + l.Debug("http cache miss", zap.String("url", req.URL.String())) } else { - l.Debug("http cache hit", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String())) + l.Debug("http cache hit", zap.String("url", req.URL.String())) } } @@ -280,7 +265,7 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo if req.Method == http.MethodGet && resp.StatusCode == http.StatusOK { cacheErr := c.baseHttpCache.Set(req, resp) if cacheErr != nil { - l.Warn("error setting cache", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()), zap.Error(cacheErr)) + l.Warn("error setting cache", zap.String("url", req.URL.String()), zap.Error(cacheErr)) } }