Commit 5815efc1 authored by hujiebin's avatar hujiebin

Update clear_redis.go

parent 8601ecaa
......@@ -4,7 +4,9 @@ import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/spf13/cast"
"log"
"strings"
)
const redisAddr = "172.28.16.31:6379" // 替换为你的Redis地址
......@@ -32,10 +34,10 @@ func main() {
println(len(keys))
// 删除所有匹配的键
//err = deleteKeys(client, keys)
//if err != nil {
// log.Fatalf("Failed to delete keys: %v", err)
//}
err = deleteKeys(client, keys)
if err != nil {
log.Fatalf("Failed to delete keys: %v", err)
}
fmt.Println("Cleanup completed.")
}
......@@ -65,14 +67,40 @@ func getKeysWithPrefix(client *redis.Client, prefix string) ([]string, error) {
}
// 删除指定的键
// 谨慎打开删除动作
func deleteKeys(client *redis.Client, keys []string) error {
ctx := context.Background()
//ctx := context.Background()
for _, key := range keys {
err := client.Del(ctx, key).Err()
if err != nil {
return err
for index, key := range keys {
if strings.Contains(key, "autochooses") {
arr := strings.Split(key, "-")
if len(arr) < 3 {
log.Fatalf("illgal")
}
if cast.ToInt(arr[2]) >= 837395 {
log.Printf("skip skip skip key:%v,index:%v\n", key, index)
continue
}
} else if strings.Contains(key, "choose_user") {
arr := strings.Split(key, ":")
if len(arr) < 3 {
log.Fatalf("illgal")
}
arr2 := strings.Split(arr[1], "-")
if len(arr2) < 3 {
log.Fatalf("illgal")
}
if cast.ToInt(arr2[2]) >= 837395 {
log.Printf("skip skip skip key:%v,index:%v\n", key, index)
continue
}
}
// 打开del操作
//err := client.Del(ctx, key).Err()
//if err != nil {
// return err
//}
log.Printf("key:%v,index:%v\n", key, index)
}
return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment