Skip to content

Commit

Permalink
Adding statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Aug 29, 2024
1 parent b25e00c commit 08a376f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewDBCache(ctx context.Context, cfg CacheConfig) (*DBCache, error) {
go func() {
ctxWithTimeout, cancel := context.WithTimeout(
ctx,
time.Duration(1)*time.Minute,
time.Duration(180)*time.Minute,
)
defer cancel()

Expand Down Expand Up @@ -633,3 +633,29 @@ func (d *DBCache) getStats(ctx context.Context) (Stats, error) {
Collisions: int64(collisions),
}, nil
}

// Len computes number of entries in cache.
func (d *DBCache) Len(ctx context.Context) (int, error) {
var count int = 0
if d.IsNilConnection() {
return -1, fmt.Errorf("%s", nilConnection)
}

l := ctxzap.Extract(ctx)
rows, err := d.db.QueryContext(ctx, `SELECT count(*) FROM http_cache`)
if err != nil {
l.Debug(errQueryingTable, zap.Error(err))
return -1, err
}

defer rows.Close()
for rows.Next() {
err = rows.Scan(&count)
if err != nil {
l.Debug("Failed to scan rows from table", zap.Error(err))
return -1, err
}
}

return count, nil
}

0 comments on commit 08a376f

Please sign in to comment.