add example for tracing feature

This commit is contained in:
jianchenma 2021-01-26 14:33:35 +08:00
parent c01b520ba6
commit 52c17dfce0
3 changed files with 10 additions and 5 deletions

View File

@ -38,6 +38,6 @@ func (m *Model) Cache(duration time.Duration, name ...string) *Model {
// cache feature is enabled.
func (m *Model) checkAndRemoveCache() {
if m.cacheEnabled && m.cacheDuration < 0 && len(m.cacheName) > 0 {
m.db.GetCache().Remove(m.cacheName)
m.db.GetCache().Ctx(m.db.GetCtx()).Remove(m.cacheName)
}
}

View File

@ -10,6 +10,7 @@ import (
"fmt"
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
@ -435,7 +436,7 @@ func (m *Model) FindScan(pointer interface{}, where ...interface{}) error {
// doGetAllBySql does the select statement on the database.
func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, err error) {
cacheKey := ""
cacheObj := m.db.GetCache()
cacheObj := m.db.GetCache().Ctx(m.db.GetCtx())
// Retrieve from cache.
if m.cacheEnabled && m.tx == nil {
cacheKey = m.cacheName
@ -461,9 +462,13 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
// Cache the result.
if cacheKey != "" && err == nil {
if m.cacheDuration < 0 {
cacheObj.Remove(cacheKey)
if _, err := cacheObj.Remove(cacheKey); err != nil {
intlog.Error(err)
}
} else {
cacheObj.Set(cacheKey, result, m.cacheDuration)
if err := cacheObj.Set(cacheKey, result, m.cacheDuration); err != nil {
intlog.Error(err)
}
}
}
return result, err

2
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/clbanning/mxj v1.8.5-0.20200714211355-ff02cfb8ea28
github.com/fsnotify/fsnotify v1.4.9
github.com/go-sql-driver/mysql v1.5.0
github.com/gogf/gcache-adapter v0.0.3
github.com/gogf/gcache-adapter v0.0.4-0.20210126062229-c84b9cefa528
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gorilla/websocket v1.4.1
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf