Skip to content

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Sep 2, 2024
1 parent 14a5f42 commit 236f33d
Showing 1 changed file with 2 additions and 38 deletions.
40 changes: 2 additions & 38 deletions pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ type ICache interface {
Set(ctx context.Context, key string, value *http.Response) error
CreateCacheKey(req *http.Request) (string, error)
}

type DBCache struct {
db *sql.DB
waitDuration int64
expirationTime int64
}

type Stats struct {
// Hits is a number of successfully found keys
Hits int64 `json:"hits"`
Expand Down Expand Up @@ -269,20 +267,12 @@ func (d *DBCache) cleanup(ctx context.Context) error {
}

l.Debug("summary and stats", zap.Any("stats", stats))
activity, err := d.getLastActivity(ctx)
err = d.close(ctx)
if err != nil {
l.Debug("error getting last activity", zap.Error(err))
l.Debug("error closing db", zap.Error(err))
return err
}

if d.Expired(activity) {
err = d.close(ctx)
if err != nil {
l.Debug("error closing db", zap.Error(err))
return err
}
}

return nil
}

Expand Down Expand Up @@ -682,29 +672,3 @@ func (d *DBCache) Len(ctx context.Context) (int, error) {

return count, nil
}

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

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

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

return maxExpiration, nil
}

0 comments on commit 236f33d

Please sign in to comment.