Skip to content

Commit

Permalink
Adding BATON_IN_MEMORY_HTTP_CACHE env
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Aug 28, 2024
1 parent e0722a2 commit db9f5be
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,17 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client)
if err != nil {
disableCache = false
}

cacheMaxSize, err := strconv.ParseInt(os.Getenv("BATON_HTTP_CACHE_MAX_SIZE"), 10, 64)
if err != nil {
cacheMaxSize = 128 // MB
}

memoryCache, err := strconv.ParseBool(os.Getenv("BATON_IN_MEMORY_HTTP_CACHE"))
if err != nil {
memoryCache = false
}

var (
config = CacheConfig{
LogDebug: l.Level().Enabled(zap.DebugLevel),
Expand All @@ -108,26 +115,37 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client)
NoExpiration: 1, // false
ExpirationTime: time.Duration(getCacheTTL()) * time.Second,
}
ok bool
ok bool
cli = &BaseHttpClient{
HttpClient: httpClient,
}
)
if v := ctx.Value(ContextKey{}); v != nil {
if config, ok = v.(CacheConfig); !ok {
return nil, fmt.Errorf("error casting config values from context")
}
}

// Set in-memory cache(NewGoCache) or db-cache(NewDBCache)
// in-memory cache
if memoryCache {
memCache, err := NewGoCache(ctx, config)
if err != nil {
l.Error("error creating http cache(in-memory)", zap.Error(err))
return nil, err
}
cli.baseHttpCache = &memCache
return cli, nil
}

// db-cache(Default)
cache, err := NewDBCache(ctx, config)
if err != nil {
l.Error("error creating http cache", zap.Error(err))
l.Error("error creating http cache(db-cache)", zap.Error(err))
return nil, err
}

var obj ICache = cache
return &BaseHttpClient{
HttpClient: httpClient,
baseHttpCache: obj,
}, nil
cli.baseHttpCache = cache
return cli, nil
}

// WithJSONResponse is a wrapper that marshals the returned response body into
Expand Down

0 comments on commit db9f5be

Please sign in to comment.