diff --git a/core/stores/mongoc/cachedmodel.go b/core/stores/mongoc/cachedmodel.go index 88142f06..21c3a11b 100644 --- a/core/stores/mongoc/cachedmodel.go +++ b/core/stores/mongoc/cachedmodel.go @@ -52,6 +52,16 @@ func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option }) } +// 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) + }) +} + // 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 cd715920..c42e5190 100644 --- a/core/stores/sqlc/cachedsql.go +++ b/core/stores/sqlc/cachedsql.go @@ -2,6 +2,7 @@ package sqlc import ( "database/sql" + "log" "time" "github.com/tal-tech/go-zero/core/stores/cache" @@ -55,6 +56,17 @@ func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedCon } } +// 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, + } +} + // DelCache deletes cache with keys. func (cc CachedConn) DelCache(keys ...string) error { return cc.cache.Del(keys...)