enhance(cache):实体的存在性检查改为使用普通Key存储。
This commit is contained in:
parent
f0c22db31f
commit
60280d0e06
63
cache/exists.go
vendored
63
cache/exists.go
vendored
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user