From db9f5bee0592253211495dd0a0a185035628ee72 Mon Sep 17 00:00:00 2001 From: mchavez Date: Wed, 28 Aug 2024 09:32:17 -0600 Subject: [PATCH] Adding BATON_IN_MEMORY_HTTP_CACHE env --- pkg/uhttp/wrapper.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index a7b54ef0..549f6b9b 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -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), @@ -108,7 +115,10 @@ 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 { @@ -116,18 +126,26 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client) } } - // 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