Skip to content

Commit

Permalink
Better function name for deleting expired cache entries. Fix stats ca…
Browse files Browse the repository at this point in the history
…lculations.
  • Loading branch information
ggreer committed Nov 9, 2024
1 parent d236ab6 commit 7807106
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func NewDBCache(ctx context.Context, cfg CacheConfig) (*DBCache, error) {
}
return
case <-ticker.C:
err := dc.remove(ctx)
err := dc.deleteExpired(ctx)
if err != nil {
l.Warn("Failed to delete expired cache entries", zap.Error(err))
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (d *DBCache) removeDB(ctx context.Context) error {
if err != nil {
return err
}
// TODO: close DB so no file handles exist and we can delete the file on windows

err = os.Remove(d.location)
if err != nil {
ctxzap.Extract(ctx).Warn("error removing cache database", zap.Error(err))
Expand Down Expand Up @@ -223,16 +223,13 @@ func (d *DBCache) Get(req *http.Request) (*http.Response, error) {
}

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

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

Expand Down Expand Up @@ -374,7 +371,7 @@ func (d *DBCache) pick(ctx context.Context, key string) ([]byte, error) {
}

// Delete all expired items from the cache.
func (d *DBCache) remove(ctx context.Context) error {
func (d *DBCache) deleteExpired(ctx context.Context) error {
if d.db == nil {
return errNilConnection
}
Expand Down

0 comments on commit 7807106

Please sign in to comment.