Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Sep 3, 2024
1 parent 343f42c commit ab76255
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
failRollback = "Failed to rollback transaction"
failInsert = "Failed to insert response data into cache table"
staticQuery = "UPDATE http_cache SET %s=(%s+1) WHERE key = ?"
failScanResponse = "Failed to scan rows for cached response"
)

func NewDBCache(ctx context.Context, cfg CacheConfig) (*DBCache, error) {
Expand Down Expand Up @@ -221,15 +222,15 @@ func (d *DBCache) Get(ctx context.Context, key string) (*http.Response, error) {

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

return resp, nil
}

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

return nil, nil
Expand Down Expand Up @@ -370,7 +371,7 @@ func (d *DBCache) Has(ctx context.Context, key string) (bool, error) {
l := ctxzap.Extract(ctx)
rows, err := d.db.Query("SELECT data FROM http_cache where key = ?", key)
if err != nil {
l.Debug("Failed to query cache table", zap.Error(err))
l.Debug("Failed to query cache table for key existence", zap.Error(err))
return false, err
}

Expand Down Expand Up @@ -405,7 +406,7 @@ func (d *DBCache) Select(ctx context.Context, key string) ([]byte, error) {
for rows.Next() {
err = rows.Scan(&data)
if err != nil {
l.Debug("Failed to scan rows from cache table", zap.Error(err))
l.Debug(failScanResponse, zap.Error(err))
return nil, err
}
}
Expand All @@ -431,7 +432,7 @@ func (d *DBCache) Remove(ctx context.Context, key string) error {
l.Debug(failRollback, zap.Error(errtx))
}

l.Debug("error deleting key", zap.Error(err))
l.Debug("Failed to delete cache key", zap.Error(err))
return err
}

Expand All @@ -441,7 +442,7 @@ func (d *DBCache) Remove(ctx context.Context, key string) error {
l.Debug(failRollback, zap.Error(errtx))
}

l.Debug("Failed to remove cache value", zap.Error(err))
l.Debug("Failed to remove cache entry", zap.Error(err))
return err
}

Expand All @@ -455,7 +456,7 @@ func (d *DBCache) close(ctx context.Context) error {

err := d.db.Close()
if err != nil {
ctxzap.Extract(ctx).Debug("error closing database", zap.Error(err))
ctxzap.Extract(ctx).Debug("Failed to close database connection", zap.Error(err))
return err
}

Expand Down Expand Up @@ -662,7 +663,7 @@ func (d *DBCache) getStats(ctx context.Context) (Stats, error) {
for rows.Next() {
err = rows.Scan(&hits, &misses, &delhits, &delmisses, &collisions)
if err != nil {
l.Debug("Failed to scan rows from cache table", zap.Error(err))
l.Debug(failScanResponse, zap.Error(err))
return Stats{}, err
}
}
Expand Down

0 comments on commit ab76255

Please sign in to comment.