enhance(cache):实体的存在性检查改为使用普通Key存储。

This commit is contained in:
徐涛 2022-09-06 12:43:04 +08:00
parent f0c22db31f
commit 60280d0e06

63
cache/exists.go vendored
View File

@ -1,72 +1,49 @@
package cache package cache
import ( import (
"electricity_bill_calc/global"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/samber/lo"
) )
func assembleExistsKey(entityName string) string { func assembleExistsKey(entityName string, additional ...string) string {
var keys = make([]string, 0) var keys = make([]string, 0)
keys = append(keys, strings.ToUpper(entityName)) keys = append(keys, strings.ToUpper(entityName))
return CacheKey(TAG_EXISTS, keys...) keys = append(keys, additional...)
} var b strings.Builder
b.WriteString(TAG_EXISTS)
func assembleExistsIdentification(additional ...string) string { for _, s := range keys {
return strings.Join(additional, ":") fmt.Fprintf(&b, ":%s", s)
}
return b.String()
} }
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
func CacheExists(relationNames []string, entityName string, conditions ...string) error { func CacheExists(relationNames []string, entityName string, conditions ...string) error {
existskey := assembleExistsKey(entityName) existskey := assembleExistsKey(entityName, conditions...)
identification := assembleExistsIdentification(conditions...) err := Cache(existskey, lo.ToPtr(true), 5*time.Minute)
cmd := global.RedisConn.B().Sadd().Key(existskey).Member(identification).Build()
err := global.RedisConn.Do(global.Ctx, cmd).Error()
for _, relationName := range relationNames { for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_SET, existskey, identification) CacheRelation(relationName, STORE_TYPE_KEY, existskey)
} }
return err return err
} }
// 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记函数在返回false时不保证数据库中相关记录也不存在 // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记函数在返回false时不保证数据库中相关记录也不存在
func CheckExists(entityName string, condtions ...string) (bool, error) { func CheckExists(entityName string, condtions ...string) (bool, error) {
existsKey := assembleExistsKey(entityName) existsKey := assembleExistsKey(entityName, condtions...)
identification := assembleExistsIdentification(condtions...) return Exists(existsKey)
cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build()
result, err := global.RedisConn.Do(global.Ctx, cmd).AsBool()
return result, err
} }
// 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记 // 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记
func AbolishExists(entityName, id string) error { func AbolishExists(entityName, id string) error {
existsKey := assembleExistsKey(entityName) pattern := fmt.Sprintf("%s:%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName), id)
pattern := fmt.Sprintf("%s*", id) return DeleteAll(pattern)
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
} }
// 从缓存中删除指定模型名称的全部存在标记 // 从缓存中删除指定模型名称的全部存在标记
func AbolishExistsEntity(entityName string) error { func AbolishExistsEntity(entityName string) error {
existskey := assembleExistsKey(entityName) pattern := fmt.Sprintf("%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName))
_, err := Delete(existskey) return DeleteAll(pattern)
return err
} }