feat(cache): add cache options with customizable expiry settings

This commit is contained in:
Chang lue Tsen
2025-05-11 04:39:22 -04:00
parent 9e20b270e7
commit 42a99c4925
3 changed files with 93 additions and 7 deletions
+16 -7
View File
@@ -5,12 +5,16 @@ import (
"database/sql"
"encoding/json"
"errors"
"time"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
var ErrNotFound = redis.Nil
var (
// ErrNotFound is the error when cache not found.
ErrNotFound = redis.Nil
)
type (
// ExecCtxFn defines the sql exec method.
@@ -23,16 +27,21 @@ type (
QueryCtxFn func(conn *gorm.DB, v interface{}) error
CachedConn struct {
db *gorm.DB
cache *redis.Client
db *gorm.DB
cache *redis.Client
expiry time.Duration
notFoundExpiry time.Duration
}
)
// NewConn returns a CachedConn with a redis cluster cache.
func NewConn(db *gorm.DB, c *redis.Client) CachedConn {
func NewConn(db *gorm.DB, c *redis.Client, opts ...Option) CachedConn {
o := newOptions(opts...)
return CachedConn{
db: db,
cache: c,
db: db,
cache: c,
expiry: o.Expiry,
notFoundExpiry: o.NotFoundExpiry,
}
}
@@ -65,7 +74,7 @@ func (cc CachedConn) SetCache(key string, v interface{}) error {
return err
}
// set redis key
return cc.cache.Set(context.Background(), key, val, 0).Err()
return cc.cache.Set(context.Background(), key, val, cc.expiry).Err()
}
// ExecCtx runs given exec on given keys, and returns execution result.