forked from free-lancers/electricity_bill_calc_service
fix(cache):修正缓存无法保存纯数字内容的问题。
This commit is contained in:
27
cache/count.go
vendored
27
cache/count.go
vendored
@@ -2,12 +2,15 @@ package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type _CountRecord struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func assembleCountKey(entityName string, additional ...string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(entityName))
|
||||
@@ -23,18 +26,32 @@ func assembleCountKey(entityName string, additional ...string) string {
|
||||
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
||||
func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error {
|
||||
countKey := assembleCountKey(entityName, conditions...)
|
||||
err := Cache(countKey, lo.ToPtr(count), 5*time.Minute)
|
||||
cacheInstance := &_CountRecord{Count: count}
|
||||
err := Cache(countKey, cacheInstance, 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, countKey)
|
||||
}
|
||||
log.Printf("[debug] [cache|save] count key: [%s], err: %+v", countKey, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
|
||||
func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
||||
countKey := assembleCountKey(entityName, condtions...)
|
||||
instance, err := Retreive[int64](countKey)
|
||||
return *instance, err
|
||||
exist, err := Exists(countKey)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
if !exist {
|
||||
return -1, nil
|
||||
}
|
||||
instance, err := Retreive[_CountRecord](countKey)
|
||||
log.Printf("[debug] [cache|retreive] count key: [%s], instance: [%v], err: %+v", countKey, instance, err)
|
||||
if instance != nil {
|
||||
return instance.Count, nil
|
||||
} else {
|
||||
return -1, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 删除指定模型名称的数量缓存
|
||||
|
Reference in New Issue
Block a user