You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/sqlc/cachedsql.go

141 lines
4.1 KiB
Go

4 years ago
package sqlc
import (
"database/sql"
"time"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/redis"
"github.com/tal-tech/go-zero/core/stores/sqlx"
"github.com/tal-tech/go-zero/core/syncx"
4 years ago
)
// see doc/sql-cache.md
const cacheSafeGapBetweenIndexAndPrimary = time.Second * 5
var (
// ErrNotFound is an alias of sqlx.ErrNotFound.
4 years ago
ErrNotFound = sqlx.ErrNotFound
// can't use one SingleFlight per conn, because multiple conns may share the same cache key.
exclusiveCalls = syncx.NewSingleFlight()
stats = cache.NewStat("sqlc")
4 years ago
)
type (
// ExecFn defines the sql exec method.
ExecFn func(conn sqlx.SqlConn) (sql.Result, error)
// IndexQueryFn defines the query method that based on unique indexes.
IndexQueryFn func(conn sqlx.SqlConn, v interface{}) (interface{}, error)
// PrimaryQueryFn defines the query method that based on primary keys.
4 years ago
PrimaryQueryFn func(conn sqlx.SqlConn, v, primary interface{}) error
// QueryFn defines the query method.
QueryFn func(conn sqlx.SqlConn, v interface{}) error
4 years ago
// A CachedConn is a DB connection with cache capability.
4 years ago
CachedConn struct {
db sqlx.SqlConn
cache cache.Cache
4 years ago
}
)
// NewNodeConn returns a CachedConn with a redis node cache.
4 years ago
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...),
4 years ago
}
}
// NewConn returns a CachedConn with a redis cluster cache.
4 years ago
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...),
4 years ago
}
}
// DelCache deletes cache with keys.
4 years ago
func (cc CachedConn) DelCache(keys ...string) error {
return cc.cache.Del(keys...)
4 years ago
}
// GetCache unmarshals cache with given key into v.
4 years ago
func (cc CachedConn) GetCache(key string, v interface{}) error {
return cc.cache.Get(key, v)
4 years ago
}
// Exec runs given exec on given keys, and returns execution result.
4 years ago
func (cc CachedConn) Exec(exec ExecFn, keys ...string) (sql.Result, error) {
res, err := exec(cc.db)
if err != nil {
return nil, err
}
if err := cc.DelCache(keys...); err != nil {
return nil, err
}
return res, nil
}
// ExecNoCache runs exec with given sql statement, without affecting cache.
4 years ago
func (cc CachedConn) ExecNoCache(q string, args ...interface{}) (sql.Result, error) {
return cc.db.Exec(q, args...)
}
// QueryRow unmarshals into v with given key and query func.
4 years ago
func (cc CachedConn) QueryRow(v interface{}, key string, query QueryFn) error {
return cc.cache.Take(v, key, func(v interface{}) error {
return query(cc.db, v)
})
}
// QueryRowIndex unmarshals into v with given key.
4 years ago
func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary interface{}) string,
indexQuery IndexQueryFn, primaryQuery PrimaryQueryFn) error {
var primaryKey interface{}
var found bool
4 years ago
4 years ago
if err := cc.cache.TakeWithExpire(&primaryKey, key, func(val interface{}, expire time.Duration) (err error) {
primaryKey, err = indexQuery(cc.db, v)
if err != nil {
return
}
found = true
return cc.cache.SetWithExpire(keyer(primaryKey), v, expire+cacheSafeGapBetweenIndexAndPrimary)
4 years ago
}); err != nil {
return err
}
if found {
return nil
}
return cc.cache.Take(v, keyer(primaryKey), func(v interface{}) error {
return primaryQuery(cc.db, v, primaryKey)
})
}
// QueryRowNoCache unmarshals into v with given statement.
4 years ago
func (cc CachedConn) QueryRowNoCache(v interface{}, q string, args ...interface{}) error {
return cc.db.QueryRow(v, q, args...)
}
// QueryRowsNoCache unmarshals into v with given statement.
// It doesn't use cache, because it might cause consistency problem.
4 years ago
func (cc CachedConn) QueryRowsNoCache(v interface{}, q string, args ...interface{}) error {
return cc.db.QueryRows(v, q, args...)
}
// SetCache sets v into cache with given key.
4 years ago
func (cc CachedConn) SetCache(key string, v interface{}) error {
return cc.cache.Set(key, v)
4 years ago
}
// Transact runs given fn in transaction mode.
4 years ago
func (cc CachedConn) Transact(fn func(sqlx.Session) error) error {
return cc.db.Transact(fn)
}