Repository: zeromicro/go-zero
Pull request #1346 (2021-12-18 14:55:33)

chore: add tests & refactor


Patch:
diff --git a/core/stores/mongoc/cachedmodel.go b/core/stores/mongoc/cachedmodel.go
index 21c3a11b..8837139d 100644
--- a/core/stores/mongoc/cachedmodel.go
+++ b/core/stores/mongoc/cachedmodel.go
@@ -36,32 +36,25 @@ func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Optio
 	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.
 func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
 	c := cache.New(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
-	return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
-		return newCollection(collection, c)
-	})
+	return NewModelWithCache(url, collection, c)
 }
 
 // NewModelWithCache returns a Model with a custom cache.
 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 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.
 func (mm *Model) Count(query interface{}) (int, error) {
 	return mm.executeInt(func(c CachedCollection) (int, error) {
diff --git a/core/stores/sqlc/cachedsql.go b/core/stores/sqlc/cachedsql.go
index c42e5190..d5f1108a 100644
--- a/core/stores/sqlc/cachedsql.go
+++ b/core/stores/sqlc/cachedsql.go
@@ -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...)


Test patch:
diff --git a/core/stores/sqlc/cachedsql_test.go b/core/stores/sqlc/cachedsql_test.go
index 5f325306..81a11100 100644
--- a/core/stores/sqlc/cachedsql_test.go
+++ b/core/stores/sqlc/cachedsql_test.go
@@ -562,6 +562,18 @@ func TestQueryRowNoCache(t *testing.T) {
 	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() {
 	atomic.StoreUint64(&stats.Total, 0)
 	atomic.StoreUint64(&stats.Hit, 0)