mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
chore: add tests & refactor (#1346)
* chore: add tests & refactor * chore: refactor
This commit is contained in:
parent
3e6c217408
commit
d1c2a31af7
@ -36,32 +36,25 @@ func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Optio
|
|||||||
return model
|
return model
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNodeModel returns a Model with a cache node.
|
|
||||||
func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
|
|
||||||
c := cache.NewNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
|
|
||||||
return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
|
|
||||||
return newCollection(collection, c)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewModel returns a Model with a cache cluster.
|
// NewModel returns a Model with a cache cluster.
|
||||||
func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
|
func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
|
||||||
c := cache.New(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
|
c := cache.New(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
|
||||||
return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
|
return NewModelWithCache(url, collection, c)
|
||||||
return newCollection(collection, c)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModelWithCache returns a Model with a custom cache.
|
// NewModelWithCache returns a Model with a custom cache.
|
||||||
func NewModelWithCache(url, collection string, c cache.Cache) (*Model, error) {
|
func NewModelWithCache(url, collection string, c cache.Cache) (*Model, error) {
|
||||||
if c == nil {
|
|
||||||
log.Fatal("Invalid cache component")
|
|
||||||
}
|
|
||||||
return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
|
return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
|
||||||
return newCollection(collection, c)
|
return newCollection(collection, c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNodeModel returns a Model with a cache node.
|
||||||
|
func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
|
||||||
|
c := cache.NewNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
|
||||||
|
return NewModelWithCache(url, collection, c)
|
||||||
|
}
|
||||||
|
|
||||||
// Count returns the count of given query.
|
// Count returns the count of given query.
|
||||||
func (mm *Model) Count(query interface{}) (int, error) {
|
func (mm *Model) Count(query interface{}) (int, error) {
|
||||||
return mm.executeInt(func(c CachedCollection) (int, error) {
|
return mm.executeInt(func(c CachedCollection) (int, error) {
|
||||||
|
@ -2,7 +2,6 @@ package sqlc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
"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.
|
// NewConn returns a CachedConn with a redis cluster cache.
|
||||||
func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
|
func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
|
||||||
return CachedConn{
|
cc := cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...)
|
||||||
db: db,
|
return NewConnWithCache(db, cc)
|
||||||
cache: cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConnWithCache returns a CachedConn with a custom cache.
|
// NewConnWithCache returns a CachedConn with a custom cache.
|
||||||
func NewConnWithCache(db sqlx.SqlConn, c cache.Cache) CachedConn {
|
func NewConnWithCache(db sqlx.SqlConn, c cache.Cache) CachedConn {
|
||||||
if c == nil {
|
|
||||||
log.Fatal("Invalid cache component")
|
|
||||||
}
|
|
||||||
return CachedConn{
|
return CachedConn{
|
||||||
db: db,
|
db: db,
|
||||||
cache: c,
|
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.
|
// DelCache deletes cache with keys.
|
||||||
func (cc CachedConn) DelCache(keys ...string) error {
|
func (cc CachedConn) DelCache(keys ...string) error {
|
||||||
return cc.cache.Del(keys...)
|
return cc.cache.Del(keys...)
|
||||||
|
@ -562,6 +562,18 @@ func TestQueryRowNoCache(t *testing.T) {
|
|||||||
assert.True(t, ran)
|
assert.True(t, ran)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewConnWithCache(t *testing.T) {
|
||||||
|
r, clean, err := redistest.CreateRedis()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer clean()
|
||||||
|
|
||||||
|
var conn trackedConn
|
||||||
|
c := NewConnWithCache(&conn, cache.NewNode(r, exclusiveCalls, stats, sql.ErrNoRows))
|
||||||
|
_, err = c.ExecNoCache("delete from user_table where id='kevin'")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, conn.execValue)
|
||||||
|
}
|
||||||
|
|
||||||
func resetStats() {
|
func resetStats() {
|
||||||
atomic.StoreUint64(&stats.Total, 0)
|
atomic.StoreUint64(&stats.Total, 0)
|
||||||
atomic.StoreUint64(&stats.Hit, 0)
|
atomic.StoreUint64(&stats.Hit, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user