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] =?UTF-8?q?enhance(cache):=E5=AE=9E=E4=BD=93=E7=9A=84?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E6=80=A7=E6=A3=80=E6=9F=A5=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=99=AE=E9=80=9AKey=E5=AD=98=E5=82=A8?= =?UTF-8?q?=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) }