Skip to content

Commit

Permalink
Code review changes part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Sep 3, 2024
1 parent 89c5a32 commit a04b8c1
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client)
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 @@ -121,25 +116,32 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client)
}
}

// 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
if !disableCache {
cacheBackend := os.Getenv("BATON_HTTP_CACHE_BACKEND")
if cacheBackend == "" {
cacheBackend = "db"
}
cli.baseHttpCache = &memCache
return cli, nil
}

// db-cache(Default)
cache, err := NewDBCache(ctx, config)
if err != nil {
l.Error("error creating http cache(db-cache)", zap.Error(err))
return nil, err
switch cacheBackend {
case "memory":
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
case "db":
cache, err := NewDBCache(ctx, config)
if err != nil {
l.Error("error creating http cache (db-cache)", zap.Error(err))
return nil, err
}
cli.baseHttpCache = cache
default:
return nil, fmt.Errorf("unsupported cache backend: %s", cacheBackend)
}
}

cli.baseHttpCache = cache
return cli, nil
}

Expand Down

0 comments on commit a04b8c1

Please sign in to comment.