Feature: support adding custom cache to mongoc and sqlc (#1313)

* merge

* Feature: support adding custom cache to mongoc and sqlc
master
MarkJoyMa 3 years ago committed by GitHub
parent b299f350be
commit 3e6c217408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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. // 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,6 +2,7 @@ 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"
@ -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. // 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...)

Loading…
Cancel
Save