From a512773f11f0854c010bcf03a0786b14506eaa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 11:13:34 +0800 Subject: [PATCH 01/13] =?UTF-8?q?fix(park):=E4=BF=AE=E5=A4=8D=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=92=8C=E4=BF=AE=E6=94=B9=E5=9B=AD=E5=8C=BA=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=9B=AD=E5=8C=BA=E4=BD=8F=E6=88=B7=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E4=B8=8D=E8=83=BD=E8=A2=AB=E4=BF=9D=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/controller/park.go b/controller/park.go index 6cf64a5..384cdc1 100644 --- a/controller/park.go +++ b/controller/park.go @@ -69,16 +69,16 @@ func listAllParksUnderSpecificUser(c *gin.Context) { } type _ParkInfoFormData struct { - Name string `json:"name" form:"name"` - Region *string `json:"region" form:"region"` - Address *string `json:"address" form:"address"` - Contact *string `json:"contact" form:"contact"` - Phone *string `json:"phone" from:"phone"` - Area decimal.NullDecimal `json:"area" from:"area"` - Capacity decimal.NullDecimal `json:"capacity" from:"capacity"` - Tenement decimal.NullDecimal `json:"tenement" from:"tenement"` - Category int `json:"category" form:"category"` - Submeter int `json:"submeter" form:"submeter"` + Name string `json:"name" form:"name"` + Region *string `json:"region" form:"region"` + Address *string `json:"address" form:"address"` + Contact *string `json:"contact" form:"contact"` + Phone *string `json:"phone" from:"phone"` + Area decimal.NullDecimal `json:"area" from:"area"` + Capacity decimal.NullDecimal `json:"capacity" from:"capacity"` + TenementQuantity decimal.NullDecimal `json:"tenement" from:"tenement"` + Category int `json:"category" form:"category"` + Submeter int `json:"submeter" form:"submeter"` } func createNewPark(c *gin.Context) { From ae056f612a23bef723f059259d466f6085a05a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 11:24:39 +0800 Subject: [PATCH 02/13] =?UTF-8?q?fix(calculate):=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E5=9B=AD=E5=8C=BA=E6=80=BB=E7=94=B5=E9=87=8F=E5=9C=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=90=8E=E7=94=B5=E5=BA=A6=E7=94=B5=E8=B4=B9=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=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 --- service/report.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/report.go b/service/report.go index b5fbc6d..b130f19 100644 --- a/service/report.go +++ b/service/report.go @@ -299,7 +299,7 @@ func (_ReportService) CalculateSummaryAndFinishStep(reportId string) error { } defer tx.Close() summary.CalculatePrices() - _, err = tx.ID(summary.ReportId).Cols("overall_price", "critical_price", "peak_price", "flat", "flat_fee", "flat_price", "valley_price").Update(summary) + _, err = tx.ID(summary.ReportId).Cols("overall_price", "critical_price", "peak_price", "flat", "flat_fee", "flat_price", "valley_price", "consumption_fee").Update(summary) if err != nil { tx.Rollback() return err From bd1033ac477bb968fa442ee1705fea7a88e7ca96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 12:28:06 +0800 Subject: [PATCH 03/13] =?UTF-8?q?enhance(cache):=E4=B8=BA=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=A2=9E=E5=8A=A060=E7=A7=92=E5=86=85=E7=9A=84?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E7=A7=92=E6=95=B0=EF=BC=8C=E4=BB=A5=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E7=BC=93=E5=AD=98=E9=9B=AA=E5=B4=A9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/abstract.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cache/abstract.go b/cache/abstract.go index 9d36c3f..1135c8a 100644 --- a/cache/abstract.go +++ b/cache/abstract.go @@ -3,6 +3,7 @@ package cache import ( "electricity_bill_calc/global" "fmt" + "math/rand" "strings" "time" @@ -23,9 +24,10 @@ const ( func Cache[T interface{}](key string, value *T, expires time.Duration) error { var err error if expires > 0 { + realExpires := expires + time.Duration(rand.Int63n(60))*time.Second setCmd := global.RedisConn.B().Set(). Key(key).Value(rueidis.JSON(value)). - ExSeconds(int64(expires.Seconds())). + ExSeconds(int64(realExpires.Seconds())). Build() err = global.RedisConn.Do(global.Ctx, setCmd).Error() } else { From 2ea84434098caa9d67131b0097d10cdc25a3b3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 12:35:53 +0800 Subject: [PATCH 04/13] =?UTF-8?q?enhance(cache):=E6=94=B9=E7=94=A8?= =?UTF-8?q?=E6=99=AE=E9=80=9AKey=E8=AE=B0=E5=BD=95=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/cache/count.go b/cache/count.go index 9f348bb..7b6d9d3 100644 --- a/cache/count.go +++ b/cache/count.go @@ -1,49 +1,44 @@ package cache import ( - "electricity_bill_calc/global" - "strconv" + "fmt" "strings" + "time" + + "github.com/samber/lo" ) -func assembleCountKey(entityName string) string { +func assembleCountKey(entityName string, additional ...string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) - return CacheKey(TAG_COUNT, keys...) -} - -func assembleCountIdentification(additional ...string) string { - return strings.Join(additional, ":") + keys = append(keys, additional...) + var b strings.Builder + b.WriteString(TAG_COUNT) + for _, s := range keys { + fmt.Fprintf(&b, ":%s", s) + } + return b.String() } // 向缓存中缓存模型名称明确的包含指定条件的实体记录数量 func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error { - countKey := assembleCountKey(entityName) - identification := assembleCountIdentification(conditions...) - cmd := global.RedisConn.B().Hset().Key(countKey).FieldValue().FieldValue(identification, strconv.FormatInt(count, 10)).Build() - result := global.RedisConn.Do(global.Ctx, cmd) + countKey := assembleCountKey(entityName, conditions...) + err := Cache(countKey, lo.ToPtr(count), 5*time.Minute) for _, relationName := range relationNames { - CacheRelation(relationName, STORE_TYPE_HASH, countKey, identification) + CacheRelation(relationName, STORE_TYPE_HASH, countKey) } - return result.Error() + return err } // 从缓存中获取模型名称明确的,包含指定条件的实体记录数量 func RetreiveCount(entityName string, condtions ...string) (int64, error) { - countKey := assembleCountKey(entityName) - identification := assembleCountIdentification(condtions...) - cmd := global.RedisConn.B().Hget().Key(countKey).Field(identification).Build() - result, err := global.RedisConn.Do(global.Ctx, cmd).AsInt64() - if err != nil { - return -1, err - } - return result, nil + countKey := assembleCountKey(entityName, condtions...) + instance, err := Retreive[int64](countKey) + return *instance, err } // 删除指定模型名称的数量缓存 func AbolishCountEntity(entityName string) error { - countKey := assembleCountKey(entityName) - cmd := global.RedisConn.B().Del().Key(countKey).Build() - err := global.RedisConn.Do(global.Ctx, cmd).Error() - return err + pattern := fmt.Sprintf("%s:%s:*", TAG_COUNT, strings.ToUpper(entityName)) + return DeleteAll(pattern) } From f0c22db31f954f2b4f776157210df09721c04cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 12:37:20 +0800 Subject: [PATCH 05/13] =?UTF-8?q?fix(cache):=E4=BF=AE=E6=AD=A3=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=85=B3=E8=81=94=E8=AE=B0=E5=BD=95=E4=B8=AD=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=9F=A5=E8=AF=A2=E6=95=B0=E9=87=8F=E7=9A=84=E9=94=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache/count.go b/cache/count.go index 7b6d9d3..c81a941 100644 --- a/cache/count.go +++ b/cache/count.go @@ -25,7 +25,7 @@ func CacheCount(relationNames []string, entityName string, count int64, conditio countKey := assembleCountKey(entityName, conditions...) err := Cache(countKey, lo.ToPtr(count), 5*time.Minute) for _, relationName := range relationNames { - CacheRelation(relationName, STORE_TYPE_HASH, countKey) + CacheRelation(relationName, STORE_TYPE_KEY, countKey) } return err } From 60280d0e066c77d048191f8e940ceaf9268269d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 12:43:04 +0800 Subject: [PATCH 06/13] =?UTF-8?q?enhance(cache):=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E7=9A=84=E5=AD=98=E5=9C=A8=E6=80=A7=E6=A3=80=E6=9F=A5=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BD=BF=E7=94=A8=E6=99=AE=E9=80=9AKey=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/exists.go | 63 ++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/cache/exists.go b/cache/exists.go index d3f8c32..cae7ad7 100644 --- a/cache/exists.go +++ b/cache/exists.go @@ -1,72 +1,49 @@ package cache import ( - "electricity_bill_calc/global" "fmt" "strings" + "time" + + "github.com/samber/lo" ) -func assembleExistsKey(entityName string) string { +func assembleExistsKey(entityName string, additional ...string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) - return CacheKey(TAG_EXISTS, keys...) -} - -func assembleExistsIdentification(additional ...string) string { - return strings.Join(additional, ":") + keys = append(keys, additional...) + var b strings.Builder + b.WriteString(TAG_EXISTS) + for _, s := range keys { + fmt.Fprintf(&b, ":%s", s) + } + return b.String() } // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 func CacheExists(relationNames []string, entityName string, conditions ...string) error { - existskey := assembleExistsKey(entityName) - identification := assembleExistsIdentification(conditions...) - cmd := global.RedisConn.B().Sadd().Key(existskey).Member(identification).Build() - err := global.RedisConn.Do(global.Ctx, cmd).Error() + existskey := assembleExistsKey(entityName, conditions...) + err := Cache(existskey, lo.ToPtr(true), 5*time.Minute) for _, relationName := range relationNames { - CacheRelation(relationName, STORE_TYPE_SET, existskey, identification) + CacheRelation(relationName, STORE_TYPE_KEY, existskey) } return err } // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在 func CheckExists(entityName string, condtions ...string) (bool, error) { - existsKey := assembleExistsKey(entityName) - identification := assembleExistsIdentification(condtions...) - cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build() - result, err := global.RedisConn.Do(global.Ctx, cmd).AsBool() - return result, err + existsKey := assembleExistsKey(entityName, condtions...) + return Exists(existsKey) } // 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记 func AbolishExists(entityName, id string) error { - existsKey := assembleExistsKey(entityName) - pattern := fmt.Sprintf("%s*", id) - var ( - err error - cursor int64 - elems = make([]string, 0) - sElem []string - ) - for { - cmd := global.RedisConn.B().Sscan().Key(existsKey).Cursor(cursor).Match(pattern).Count(20).Build() - result := global.RedisConn.Do(global.Ctx, cmd) - cursor, sElem, err = dissembleScan(result) - if err != nil { - return err - } - elems = append(elems, sElem...) - if cursor == 0 { - break - } - } - cmd := global.RedisConn.B().Srem().Key(existsKey).Member(elems...).Build() - err = global.RedisConn.Do(global.Ctx, cmd).Error() - return err + pattern := fmt.Sprintf("%s:%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName), id) + return DeleteAll(pattern) } // 从缓存中删除指定模型名称的全部存在标记 func AbolishExistsEntity(entityName string) error { - existskey := assembleExistsKey(entityName) - _, err := Delete(existskey) - return err + pattern := fmt.Sprintf("%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName)) + return DeleteAll(pattern) } From 2a07db75c70206eb684c47479ac6ec01d6d35d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 13:17:59 +0800 Subject: [PATCH 07/13] =?UTF-8?q?enhance(cache):=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/repository.go | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 cache/repository.go diff --git a/cache/repository.go b/cache/repository.go deleted file mode 100644 index 8242c0d..0000000 --- a/cache/repository.go +++ /dev/null @@ -1,33 +0,0 @@ -package cache - -import ( - "electricity_bill_calc/config" -) - -func CacheData[T interface{}](instance T, category string, key ...string) error { - var keys = make([]string, 0) - keys = append(keys, category) - keys = append(keys, key...) - cacheKey := CacheKey("cache", keys...) - if exists, _ := Exists(cacheKey); exists { - Delete(cacheKey) - } - return Cache(cacheKey, &instance, config.ServiceSettings.CacheLifeTime) -} - -func RetreiveData[T interface{}](category string, key ...string) (*T, error) { - var keys = make([]string, 0) - keys = append(keys, category) - keys = append(keys, key...) - return Retreive[T](CacheKey("cache", keys...)) -} - -func AbolishCacheData(category string, key ...string) { - var keys = make([]string, 0) - keys = append(keys, category) - keys = append(keys, key...) - cacheKey := CacheKey("cache", keys...) - if exists, _ := Exists(cacheKey); exists { - Delete(cacheKey) - } -} From a1e9167cdf47acd96e571df462e6a4dc25c356ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 13:56:30 +0800 Subject: [PATCH 08/13] =?UTF-8?q?enhance(cache):=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=85=B3=E8=81=94=E5=85=B3=E7=B3=BB=E4=B8=AD?= =?UTF-8?q?=E6=97=A0=E6=95=88=E9=94=AE=E6=B8=85=E7=90=86=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/relation.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 12 ++++++++++++ 2 files changed, 62 insertions(+) diff --git a/cache/relation.go b/cache/relation.go index 2333dde..1bb8ff9 100644 --- a/cache/relation.go +++ b/cache/relation.go @@ -2,6 +2,7 @@ package cache import ( "electricity_bill_calc/global" + "fmt" "strings" "github.com/rueian/rueidis" @@ -69,3 +70,52 @@ func AbolishRelation(relationName string) error { return nil } } + +func ClearOrphanRelationItems() error { + var ( + err error + cursor int64 + keys = make([]string, 0) + sKeys []string + ) + for { + scanCmd := global.RedisConn.B().Scan().Cursor(cursor).Match(fmt.Sprintf("%s:*", TAG_RELATION)).Count(20).Build() + results := global.RedisConn.Do(global.Ctx, scanCmd) + cursor, sKeys, err = dissembleScan(results) + if err != nil { + return err + } + keys = append(keys, sKeys...) + if cursor == 0 { + break + } + } + var cmds = make(rueidis.Commands, 0) + for _, key := range keys { + relationItemsCmd := global.RedisConn.B().Smembers().Key(key).Build() + results := global.RedisConn.Do(global.Ctx, relationItemsCmd) + relationItems, err := results.AsStrSlice() + if err != nil { + return err + } + for _, item := range relationItems { + exist, err := Exists(item) + if err != nil { + return err + } + if !exist { + cmd := global.RedisConn.B().Srem().Key(key).Member(item).Build() + cmds = append(cmds, cmd) + } + } + } + errs := global.RedisConn.DoMulti(global.Ctx, cmds...) + firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool { + return elem.Error() != nil + }) + if has { + return firstErr.Error() + } else { + return nil + } +} diff --git a/main.go b/main.go index e617cf8..2332316 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "electricity_bill_calc/cache" "electricity_bill_calc/config" "electricity_bill_calc/global" "electricity_bill_calc/model" @@ -157,8 +158,19 @@ func DBConnectionKeepLive() { } } +func RedisOrphanCleanup() { + for range time.Tick(2 * time.Minute) { + err := cache.ClearOrphanRelationItems() + if err != nil { + log.Printf("[Cache] [Cleanup] Orphan keys clear failed: %v", err) + continue + } + } +} + func main() { go DBConnectionKeepLive() + go RedisOrphanCleanup() gin.SetMode(config.ServerSettings.RunMode) r := router.Router() r.Run(fmt.Sprintf(":%d", config.ServerSettings.HttpPort)) 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 09/13] =?UTF-8?q?fix(cache):=E4=BF=AE=E6=AD=A3=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=97=A0=E6=B3=95=E4=BF=9D=E5=AD=98=E7=BA=AF=E6=95=B0?= =?UTF-8?q?=E5=AD=97=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() From a4a9938675dc982bfc1109ecf41ca8df1a19ef4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 15:12:32 +0800 Subject: [PATCH 10/13] =?UTF-8?q?enhance(cache):=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=85=B3=E7=B3=BB=E6=B8=85=E7=90=86=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/relation.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cache/relation.go b/cache/relation.go index 1bb8ff9..007c254 100644 --- a/cache/relation.go +++ b/cache/relation.go @@ -98,16 +98,18 @@ func ClearOrphanRelationItems() error { if err != nil { return err } + keysNeedRemove := make([]string, 0) for _, item := range relationItems { exist, err := Exists(item) if err != nil { return err } if !exist { - cmd := global.RedisConn.B().Srem().Key(key).Member(item).Build() - cmds = append(cmds, cmd) + keysNeedRemove = append(keysNeedRemove, item) } } + cmd := global.RedisConn.B().Srem().Key(key).Member(keysNeedRemove...).Build() + cmds = append(cmds, cmd) } errs := global.RedisConn.DoMulti(global.Ctx, cmds...) firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool { From 954285e4268a4c87696625eb3b5bfd5d401303fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 15:19:32 +0800 Subject: [PATCH 11/13] =?UTF-8?q?fix(cache):=E6=B8=85=E9=99=A4=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E4=B8=8D=E7=94=A8=E7=9A=84=E8=B0=83=E8=AF=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cache/count.go b/cache/count.go index b0958bc..b1baa54 100644 --- a/cache/count.go +++ b/cache/count.go @@ -2,7 +2,6 @@ package cache import ( "fmt" - "log" "strings" "time" ) @@ -31,7 +30,6 @@ func CacheCount(relationNames []string, entityName string, count int64, conditio for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, countKey) } - log.Printf("[debug] [cache|save] count key: [%s], err: %+v", countKey, err) return err } @@ -46,11 +44,10 @@ func RetreiveCount(entityName string, condtions ...string) (int64, error) { 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 { + if instance != nil && err == nil { return instance.Count, nil } else { - return -1, nil + return -1, err } } From 8aa4e38760b23df661baa1e053c07cc18e8094b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 15:28:27 +0800 Subject: [PATCH 12/13] =?UTF-8?q?fix(park):=E4=BF=AE=E5=A4=8D=E5=9B=AD?= =?UTF-8?q?=E5=8C=BA=E4=B8=8D=E8=83=BD=E6=90=9C=E7=B4=A2=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 6 ++++-- service/park.go | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/controller/park.go b/controller/park.go index 384cdc1..ba47c95 100644 --- a/controller/park.go +++ b/controller/park.go @@ -49,7 +49,8 @@ func listAllParksUnderSessionUser(c *gin.Context) { result.Unauthorized(err.Error()) return } - parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid) + keyword := c.DefaultQuery("keyword", "") + parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid, keyword) if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return @@ -60,7 +61,8 @@ func listAllParksUnderSessionUser(c *gin.Context) { func listAllParksUnderSpecificUser(c *gin.Context) { result := response.NewResult(c) requestUserId := c.Param("uid") - parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId) + keyword := c.DefaultQuery("keyword", "") + parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId, keyword) if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return diff --git a/service/park.go b/service/park.go index 15b1d99..cbfeefc 100644 --- a/service/park.go +++ b/service/park.go @@ -73,19 +73,29 @@ func (_ParkService) DeletePark(uid, pid string) error { return nil } -func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) { - if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid); parks != nil { +func (_ParkService) ListAllParkBelongsTo(uid, keyword string) ([]model.Park, error) { + if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid, keyword); parks != nil { return *parks, nil } + cond := builder.NewCond().And(builder.Eq{"user_id": uid}) + if len(keyword) > 0 { + cond = cond.And( + builder.Like{"name", keyword}. + Or(builder.Like{"abbr", keyword}). + Or(builder.Like{"address", keyword}). + Or(builder.Like{"contact", keyword}). + Or(builder.Like{"phone", keyword}), + ) + } parks := make([]model.Park, 0) err := global.DBConn. - Where(builder.Eq{"user_id": uid}). + Where(cond). NoAutoCondition(). Find(&parks) if err != nil { return make([]model.Park, 0), err } - cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid) + cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid, keyword) return parks, nil } From 0d2b1431b69ea96af5f670161f4b400e648939cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 6 Sep 2022 15:53:24 +0800 Subject: [PATCH 13/13] =?UTF-8?q?fix(cache):=E4=BF=AE=E5=A4=8D=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=85=B3=E8=81=94=E5=85=B3=E7=B3=BB=E8=A2=AB=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E6=B8=85=E9=99=A4=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/relation.go | 9 ++++----- main.go | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cache/relation.go b/cache/relation.go index 007c254..f50f5be 100644 --- a/cache/relation.go +++ b/cache/relation.go @@ -98,18 +98,17 @@ func ClearOrphanRelationItems() error { if err != nil { return err } - keysNeedRemove := make([]string, 0) for _, item := range relationItems { - exist, err := Exists(item) + separated := strings.Split(item, ";") + exist, err := Exists(separated[1]) if err != nil { return err } if !exist { - keysNeedRemove = append(keysNeedRemove, item) + cmd := global.RedisConn.B().Srem().Key(key).Member(item).Build() + cmds = append(cmds, cmd) } } - cmd := global.RedisConn.B().Srem().Key(key).Member(keysNeedRemove...).Build() - cmds = append(cmds, cmd) } errs := global.RedisConn.DoMulti(global.Ctx, cmds...) firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool { diff --git a/main.go b/main.go index 2332316..5f10237 100644 --- a/main.go +++ b/main.go @@ -160,6 +160,7 @@ func DBConnectionKeepLive() { func RedisOrphanCleanup() { for range time.Tick(2 * time.Minute) { + log.Printf("[Cache] [Cleanup] Proceeding cleanup orphan keys.") err := cache.ClearOrphanRelationItems() if err != nil { log.Printf("[Cache] [Cleanup] Orphan keys clear failed: %v", err)