chore: add tests & refactor (#1346)

* chore: add tests & refactor

* chore: refactor
This commit is contained in:
Kevin Wan
2021-12-18 23:11:38 +08:00
committed by GitHub
parent 3e6c217408
commit d1c2a31af7
3 changed files with 27 additions and 30 deletions

View File

@@ -2,7 +2,6 @@ package sqlc
import (
"database/sql"
"log"
"time"
"github.com/tal-tech/go-zero/core/stores/cache"
@@ -40,33 +39,26 @@ type (
}
)
// NewNodeConn returns a CachedConn with a redis node cache.
func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn {
return CachedConn{
db: db,
cache: cache.NewNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...),
}
}
// NewConn returns a CachedConn with a redis cluster cache.
func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
return CachedConn{
db: db,
cache: cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...),
}
cc := cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...)
return NewConnWithCache(db, cc)
}
// NewConnWithCache returns a CachedConn with a custom cache.
func NewConnWithCache(db sqlx.SqlConn, c cache.Cache) CachedConn {
if c == nil {
log.Fatal("Invalid cache component")
}
return CachedConn{
db: db,
cache: c,
}
}
// NewNodeConn returns a CachedConn with a redis node cache.
func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn {
c := cache.NewNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...)
return NewConnWithCache(db, c)
}
// DelCache deletes cache with keys.
func (cc CachedConn) DelCache(keys ...string) error {
return cc.cache.Del(keys...)