fix(cache):修正缓存无法保存纯数字内容的问题。

This commit is contained in:
徐涛 2022-09-06 15:06:34 +08:00
parent a1e9167cdf
commit 8a21a2f469
8 changed files with 31 additions and 14 deletions

27
cache/count.go vendored
View File

@ -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
}
}
// 删除指定模型名称的数量缓存

View File

@ -205,7 +205,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("charge_with_name", condition...); cachedTotal != -1 {
if cachedTotal, err := cache.RetreiveCount("charge_with_name", condition...); cachedTotal != -1 && err == nil {
total = cachedTotal
} else {
total, err = global.DBConn.

View File

@ -45,7 +45,7 @@ func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) (
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("end_user_detail", conditions...); cachedTotal != -1 {
if cachedTotal, err := cache.RetreiveCount("end_user_detail", conditions...); cachedTotal != -1 && err == nil {
total = cachedTotal
} else {
total, err = global.DBConn.

View File

@ -36,7 +36,7 @@ func (_Meter04kVService) ListMeterDetail(park, keyword string, page int) ([]mode
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("meter_04kv", condition...); cachedTotal != -1 {
if cachedTotal, err := cache.RetreiveCount("meter_04kv", condition...); cachedTotal != -1 && err == nil {
total = cachedTotal
} else {
total, err = global.DBConn.Where(cond).NoAutoCondition().Count(new(model.Meter04KV))

View File

@ -461,7 +461,7 @@ func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword stri
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 {
if cachedTotal, err := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 && err == nil {
total = cachedTotal
} else {
total, err := global.DBConn.

View File

@ -14,7 +14,7 @@ type _StatisticsService struct{}
var StatisticsService _StatisticsService
func (_StatisticsService) EnabledEnterprises() (int64, error) {
if cachedCount, _ := cache.RetreiveCount("enabled_ent"); cachedCount != -1 {
if cachedCount, err := cache.RetreiveCount("enabled_ent"); cachedCount != -1 && err == nil {
return cachedCount, nil
}
c, err := global.DBConn.
@ -28,7 +28,7 @@ func (_StatisticsService) EnabledEnterprises() (int64, error) {
}
func (_StatisticsService) EnabledParks(userIds ...string) (int64, error) {
if cachedParks, _ := cache.RetreiveCount("enabled_parks", userIds...); cachedParks != -1 {
if cachedParks, err := cache.RetreiveCount("enabled_parks", userIds...); cachedParks != -1 && err == nil {
return cachedParks, nil
}
cond := builder.NewCond().And(builder.Eq{"enabled": true})

View File

@ -342,7 +342,7 @@ func (_UserService) ListUserDetail(keyword string, userType int, userState *bool
total int64
err error
)
if cacheCounts, _ := cache.RetreiveCount("join_user_detail", cacheConditions...); cacheCounts != -1 {
if cacheCounts, err := cache.RetreiveCount("join_user_detail", cacheConditions...); cacheCounts != -1 && err == nil {
total = cacheCounts
} else {
total, err = global.DBConn.

View File

@ -85,7 +85,7 @@ func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]m
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 {
if cachedTotal, err := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 && err == nil {
total = cachedTotal
} else {
total, err = global.DBConn.
@ -134,7 +134,7 @@ func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
}
func (_WithdrawService) AuditWaits() (int64, error) {
if cachedWaits, _ := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 {
if cachedWaits, err := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 && err == nil {
return cachedWaits, nil
}
cond := builder.NewCond()