2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
import (
|
2022-03-24 17:51:49 +08:00
|
|
|
"context"
|
2020-03-13 17:21:30 +08:00
|
|
|
"time"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2020-03-13 17:21:30 +08:00
|
|
|
)
|
|
|
|
|
2022-11-03 20:22:36 +08:00
|
|
|
// CacheOption is options for model cache control in query.
|
2021-11-09 16:06:31 +08:00
|
|
|
type CacheOption struct {
|
|
|
|
// Duration is the TTL for the cache.
|
|
|
|
// If the parameter `Duration` < 0, which means it clear the cache with given `Name`.
|
|
|
|
// If the parameter `Duration` = 0, which means it never expires.
|
|
|
|
// If the parameter `Duration` > 0, which means it expires after `Duration`.
|
|
|
|
Duration time.Duration
|
|
|
|
|
|
|
|
// Name is an optional unique name for the cache.
|
|
|
|
// The Name is used to bind a name to the cache, which means you can later control the cache
|
|
|
|
// like changing the `duration` or clearing the cache with specified Name.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Force caches the query result whatever the result is nil or not.
|
|
|
|
// It is used to avoid Cache Penetration.
|
|
|
|
Force bool
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:15:44 +08:00
|
|
|
// selectCacheItem is the cache item for SELECT statement result.
|
2022-03-31 15:42:12 +08:00
|
|
|
type selectCacheItem struct {
|
2022-03-31 16:15:44 +08:00
|
|
|
Result Result // Sql result of SELECT statement.
|
|
|
|
FirstResultColumn string // The first column name of result, for Value/Count functions.
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// Cache sets the cache feature for the model. It caches the result of the sql, which means
|
|
|
|
// if there's another same sql request, it just reads and returns the result from cache, it
|
|
|
|
// but not committed and executed into the database.
|
|
|
|
//
|
2020-04-26 21:31:55 +08:00
|
|
|
// Note that, the cache feature is disabled if the model is performing select statement
|
|
|
|
// on a transaction.
|
2021-11-09 16:06:31 +08:00
|
|
|
func (m *Model) Cache(option CacheOption) *Model {
|
2020-03-13 17:21:30 +08:00
|
|
|
model := m.getModel()
|
2021-11-09 16:06:31 +08:00
|
|
|
model.cacheOption = option
|
2020-04-26 21:31:55 +08:00
|
|
|
model.cacheEnabled = true
|
2020-03-13 17:21:30 +08:00
|
|
|
return model
|
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
|
2022-03-31 15:42:12 +08:00
|
|
|
// checkAndRemoveSelectCache checks and removes the cache in insert/update/delete statement if
|
2020-04-26 21:31:55 +08:00
|
|
|
// cache feature is enabled.
|
2022-03-31 15:42:12 +08:00
|
|
|
func (m *Model) checkAndRemoveSelectCache(ctx context.Context) {
|
2021-11-09 16:06:31 +08:00
|
|
|
if m.cacheEnabled && m.cacheOption.Duration < 0 && len(m.cacheOption.Name) > 0 {
|
2022-09-26 22:11:13 +08:00
|
|
|
var cacheKey = m.makeSelectCacheKey("")
|
|
|
|
if _, err := m.db.GetCache().Remove(ctx, cacheKey); err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(ctx, `%+v`, err)
|
2021-09-17 10:48:08 +08:00
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-31 15:42:12 +08:00
|
|
|
|
|
|
|
func (m *Model) getSelectResultFromCache(ctx context.Context, sql string, args ...interface{}) (result Result, err error) {
|
|
|
|
if !m.cacheEnabled || m.tx != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
cacheItem *selectCacheItem
|
|
|
|
cacheKey = m.makeSelectCacheKey(sql, args...)
|
|
|
|
cacheObj = m.db.GetCache()
|
2024-04-16 19:31:06 +08:00
|
|
|
core = m.db.GetCore()
|
2022-03-31 15:42:12 +08:00
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
if cacheItem != nil {
|
2024-04-16 19:31:06 +08:00
|
|
|
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
|
2022-04-19 10:42:16 +08:00
|
|
|
if cacheItem.FirstResultColumn != "" {
|
2022-03-31 15:42:12 +08:00
|
|
|
internalData.FirstResultColumn = cacheItem.FirstResultColumn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if v, _ := cacheObj.Get(ctx, cacheKey); !v.IsNil() {
|
2024-03-07 11:36:42 +08:00
|
|
|
if err = v.Scan(&cacheItem); err != nil {
|
2022-11-16 10:04:49 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cacheItem.Result, nil
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:04:49 +08:00
|
|
|
func (m *Model) saveSelectResultToCache(
|
|
|
|
ctx context.Context, queryType queryType, result Result, sql string, args ...interface{},
|
|
|
|
) (err error) {
|
2022-03-31 15:42:12 +08:00
|
|
|
if !m.cacheEnabled || m.tx != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
cacheKey = m.makeSelectCacheKey(sql, args...)
|
|
|
|
cacheObj = m.db.GetCache()
|
|
|
|
)
|
|
|
|
if m.cacheOption.Duration < 0 {
|
|
|
|
if _, errCache := cacheObj.Remove(ctx, cacheKey); errCache != nil {
|
|
|
|
intlog.Errorf(ctx, `%+v`, errCache)
|
|
|
|
}
|
2022-11-16 10:04:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Special handler for Value/Count operations result.
|
|
|
|
if len(result) > 0 {
|
2024-04-16 19:31:06 +08:00
|
|
|
var core = m.db.GetCore()
|
2022-11-16 10:04:49 +08:00
|
|
|
switch queryType {
|
|
|
|
case queryTypeValue, queryTypeCount:
|
2024-04-16 19:31:06 +08:00
|
|
|
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
|
2022-11-16 10:04:49 +08:00
|
|
|
if result[0][internalData.FirstResultColumn].IsEmpty() {
|
|
|
|
result = nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|
2022-11-16 10:04:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case of Cache Penetration.
|
|
|
|
if result.IsEmpty() {
|
|
|
|
if m.cacheOption.Force {
|
|
|
|
result = Result{}
|
|
|
|
} else {
|
|
|
|
result = nil
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 19:31:06 +08:00
|
|
|
var (
|
|
|
|
core = m.db.GetCore()
|
|
|
|
cacheItem = &selectCacheItem{
|
|
|
|
Result: result,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
|
2022-11-16 10:04:49 +08:00
|
|
|
cacheItem.FirstResultColumn = internalData.FirstResultColumn
|
|
|
|
}
|
|
|
|
if errCache := cacheObj.Set(ctx, cacheKey, cacheItem, m.cacheOption.Duration); errCache != nil {
|
|
|
|
intlog.Errorf(ctx, `%+v`, errCache)
|
|
|
|
}
|
|
|
|
return
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) makeSelectCacheKey(sql string, args ...interface{}) string {
|
2024-05-22 21:14:43 +08:00
|
|
|
var (
|
|
|
|
table = m.db.GetCore().guessPrimaryTableName(m.tables)
|
|
|
|
group = m.db.GetGroup()
|
|
|
|
schema = m.db.GetSchema()
|
|
|
|
customName = m.cacheOption.Name
|
|
|
|
)
|
|
|
|
return genSelectCacheKey(
|
|
|
|
table,
|
|
|
|
group,
|
|
|
|
schema,
|
|
|
|
customName,
|
2022-09-26 22:11:13 +08:00
|
|
|
sql,
|
|
|
|
args...,
|
|
|
|
)
|
2022-03-31 15:42:12 +08:00
|
|
|
}
|