From 8a21a2f46912c93122aa5507d40261fd4cb47307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 15:06:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(cache):=E4=BF=AE=E6=AD=A3=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E4=BF=9D=E5=AD=98=E7=BA=AF=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 27 ++++++++++++++++++++++----- service/charge.go | 2 +- service/end_user.go | 2 +- service/meter04kv.go | 2 +- service/report.go | 2 +- service/statistics.go | 4 ++-- service/user.go | 2 +- service/withdraw.go | 4 ++-- 8 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cache/count.go b/cache/count.go index c81a941..b0958bc 100644 --- a/cache/count.go +++ b/cache/count.go @@ -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 + } } // 删除指定模型名称的数量缓存 diff --git a/service/charge.go b/service/charge.go index 3c575a2..8c31265 100644 --- a/service/charge.go +++ b/service/charge.go @@ -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. diff --git a/service/end_user.go b/service/end_user.go index 96ae9b6..3c92ab7 100644 --- a/service/end_user.go +++ b/service/end_user.go @@ -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. diff --git a/service/meter04kv.go b/service/meter04kv.go index 9934cc0..1ed540f 100644 --- a/service/meter04kv.go +++ b/service/meter04kv.go @@ -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)) diff --git a/service/report.go b/service/report.go index b130f19..08726a4 100644 --- a/service/report.go +++ b/service/report.go @@ -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. diff --git a/service/statistics.go b/service/statistics.go index 368eba1..0b9a528 100644 --- a/service/statistics.go +++ b/service/statistics.go @@ -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}) diff --git a/service/user.go b/service/user.go index 83cc44c..3e4dc93 100644 --- a/service/user.go +++ b/service/user.go @@ -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. diff --git a/service/withdraw.go b/service/withdraw.go index 39d68f3..84f4ac1 100644 --- a/service/withdraw.go +++ b/service/withdraw.go @@ -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()