Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Aug 27, 2024
1 parent c1ffcf1 commit 041395f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/uhttp/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 041395f

Please sign in to comment.