Skip to content

Commit

Permalink
Code review changes part 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Sep 3, 2024
1 parent 9492a92 commit 769b4b7
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type DBCache struct {
expirationTime int64
// Database path
location string
// enable statistics
stats bool
}
type Stats struct {
// Hits is a number of successfully found keys
Expand Down Expand Up @@ -85,6 +87,7 @@ func NewDBCache(ctx context.Context, cfg CacheConfig) (*DBCache, error) {
}

if cfg.CacheTTL > 0 {
dc.stats = !cfg.DisableCache
if cfg.CacheTTL > cacheTTLThreshold {
dc.waitDuration = int64(cfg.CacheTTL * cacheTTLMultiplier) // set as a fraction of the Cache TTL
}
Expand Down Expand Up @@ -164,32 +167,40 @@ func (d *DBCache) removeDB(ctx context.Context) error {

// Get returns cached response (if exists).
func (d *DBCache) Get(ctx context.Context, key string) (*http.Response, error) {
var (
isFound bool = false
resp *http.Response
)
if d.IsNilConnection() {
return nil, fmt.Errorf("%s", nilConnection)
}

entry, err := d.pick(ctx, key)
if err == nil && len(entry) > 0 {
r := bufio.NewReader(bytes.NewReader(entry))
resp, err := http.ReadResponse(r, nil)
resp, err = http.ReadResponse(r, nil)
if err != nil {
return nil, err
}

err = d.hits(ctx, key)
if err != nil {
ctxzap.Extract(ctx).Debug("Failed to update cache hits", zap.Error(err))
}

return resp, nil
isFound = true
}

err = d.misses(ctx, key)
if err != nil {
ctxzap.Extract(ctx).Debug("Failed to update cache misses", zap.Error(err))
if d.stats {
if isFound {
err = d.hits(ctx, key)
if err != nil {
ctxzap.Extract(ctx).Debug("Failed to update cache hits", zap.Error(err))
}
}

err = d.misses(ctx, key)
if err != nil {
ctxzap.Extract(ctx).Debug("Failed to update cache misses", zap.Error(err))
}
}

return nil, nil
return resp, nil
}

// Set stores and save response in the db.
Expand Down

0 comments on commit 769b4b7

Please sign in to comment.