From 041395f9e6f0a3895b5bcfb6e739aa1b6c5e965c Mon Sep 17 00:00:00 2001 From: mchavez Date: Tue, 27 Aug 2024 12:03:25 -0600 Subject: [PATCH] Code review changes --- pkg/uhttp/dbcache.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/uhttp/dbcache.go b/pkg/uhttp/dbcache.go index 85fbfe88..3eac7594 100644 --- a/pkg/uhttp/dbcache.go +++ b/pkg/uhttp/dbcache.go @@ -67,7 +67,10 @@ func NewDBCache(ctx context.Context, cfg CacheConfig) (*DBCache, error) { select { case <-ctx.Done(): // ctx done, shutting down cache cleanup routine - dc.Clear(ctx) + err := dc.Clear(ctx) + if err != nil { + l.Debug("shutting down cache failed", zap.Error(err)) + } return case <-ticker.C: err := dc.DeleteExpired(ctx) @@ -114,6 +117,7 @@ func (d *DBCache) CreateCacheKey(req *http.Request) (string, error) { return cacheString, nil } +// Get returns cached response (if exists). func (d *DBCache) Get(ctx context.Context, key string) (*http.Response, error) { if d.IsNilConnection() { return nil, fmt.Errorf("database connection is nil") @@ -133,6 +137,7 @@ func (d *DBCache) Get(ctx context.Context, key string) (*http.Response, error) { return nil, nil } +// Set stores and save response in the db. func (d *DBCache) Set(ctx context.Context, key string, value *http.Response) error { if d.IsNilConnection() { return fmt.Errorf("database connection is nil") @@ -151,6 +156,7 @@ func (d *DBCache) Set(ctx context.Context, key string, value *http.Response) err return nil } +// Remove stored keys. func (d *DBCache) Delete(ctx context.Context, key string) error { if d.IsNilConnection() { return fmt.Errorf("database connection is nil") @@ -212,6 +218,7 @@ func (d *DBCache) Insert(ctx context.Context, key string, value any) error { return nil } +// Has query for cached keys. func (d *DBCache) Has(ctx context.Context, key string) (bool, error) { if d.IsNilConnection() { return false, fmt.Errorf("database connection is nil") @@ -237,6 +244,7 @@ func (d *DBCache) IsNilConnection() bool { return d.db == nil } +// Select query for cached response. func (d *DBCache) Select(ctx context.Context, key string) ([]byte, error) { var data []byte if d.IsNilConnection() { @@ -293,6 +301,7 @@ func (d *DBCache) close(ctx context.Context) error { return nil } +// Expired checks if key is expired. func (d *DBCache) Expired(expiration int64) bool { return time.Now().UnixNano() > expiration }