enhance(cache):增加一个批量删除指定模式键的函数。

This commit is contained in:
徐涛 2022-08-12 14:43:30 +08:00
parent 447a88ceac
commit 8294e1f546

22
cache/abstract.go vendored
View File

@ -59,6 +59,28 @@ func Delete(key string) (bool, error) {
return result > 0, nil
}
// 从Redis缓存中批量删除符合pattern的键这里的pattern直接使用Redis的pattern规则
func DeleteAll(pattern string) error {
var (
cursor uint64
keys = make([]string, 0)
)
for {
k, cursor, err := global.RedisConn.Scan(global.Ctx, cursor, pattern, 20).Result()
if err != nil {
return err
}
keys = append(keys, k...)
if cursor == 0 {
break
}
}
pipeline := global.RedisConn.Pipeline()
pipeline.Del(global.Ctx, keys...)
_, err := pipeline.Exec(global.Ctx)
return err
}
// 生成用于Redis存储的键
func CacheKey(category string, ids ...string) string {
var b strings.Builder