enhance(cache):精简存在性缓存的键。

This commit is contained in:
徐涛 2022-08-25 10:09:16 +08:00
parent dba8f6f3ca
commit f58c05d2fb

11
cache/exists.go vendored
View File

@ -13,9 +13,8 @@ func assembleExistsKey(entityName string) string {
return CacheKey(TAG_EXISTS, keys...) return CacheKey(TAG_EXISTS, keys...)
} }
func assembleExistsIdentification(id string, additional ...string) string { func assembleExistsIdentification(additional ...string) string {
var b strings.Builder var b strings.Builder
b.WriteString(id)
for _, s := range additional { for _, s := range additional {
fmt.Fprintf(&b, ":%s", s) fmt.Fprintf(&b, ":%s", s)
} }
@ -23,17 +22,17 @@ func assembleExistsIdentification(id string, additional ...string) string {
} }
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
func CacheExists(entityName, id string, conditions ...string) error { func CacheExists(entityName string, conditions ...string) error {
existskey := assembleExistsKey(entityName) existskey := assembleExistsKey(entityName)
identification := assembleExistsIdentification(id, conditions...) identification := assembleExistsIdentification(conditions...)
result := global.RedisConn.SAdd(global.Ctx, existskey, identification) result := global.RedisConn.SAdd(global.Ctx, existskey, identification)
return result.Err() return result.Err()
} }
// 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记函数在返回false时不保证数据库中相关记录也不存在 // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记函数在返回false时不保证数据库中相关记录也不存在
func CheckExists(entityName, id string, condtions ...string) (bool, error) { func CheckExists(entityName string, condtions ...string) (bool, error) {
existsKey := assembleExistsKey(entityName) existsKey := assembleExistsKey(entityName)
identification := assembleExistsIdentification(id, condtions...) identification := assembleExistsIdentification(condtions...)
result := global.RedisConn.SIsMember(global.Ctx, existsKey, identification) result := global.RedisConn.SIsMember(global.Ctx, existsKey, identification)
return result.Val(), result.Err() return result.Val(), result.Err()
} }