Skip to content

Commit

Permalink
Move cache config stuff into cache file. Don't create cachekey since …
Browse files Browse the repository at this point in the history
…get/set cache just needs req. Add tostring method to cache config.
  • Loading branch information
ggreer committed Sep 13, 2024
1 parent cc17bc3 commit d6c6b32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
22 changes: 21 additions & 1 deletion pkg/uhttp/gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -94,24 +111,27 @@ 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))
return nil, err
}
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))
Expand Down
25 changes: 5 additions & 20 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()))
}
}

Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit d6c6b32

Please sign in to comment.