package main import ( "context" "fmt" "github.com/go-redis/redis/v8" "log" ) const redisAddr = "172.28.16.47:6379" // 替换为你的Redis地址 func main() { // 创建Redis客户端 client := redis.NewClient(&redis.Options{ Addr: redisAddr, DB: 0, // 替换为你的数据库编号 //Password: "yPyZH1DYMJhrVQgr", }) // 关闭连接 defer func() { if err := client.Close(); err != nil { log.Fatalf("Failed to close Redis connection: %v", err) } }() // 获取所有以"user_qps_"为前缀的键 keys, err := getKeysWithPrefix(client, "game:level:points:") if err != nil { log.Fatalf("Failed to get keys with prefix: %v", err) } println(len(keys)) // 谨慎打开-删除所有匹配的键 //err = deleteKeys(client, keys) //if err != nil { // log.Fatalf("Failed to delete keys: %v", err) //} fmt.Println("Cleanup completed.") } // 获取所有以特定前缀开头的键 func getKeysWithPrefix(client *redis.Client, prefix string) ([]string, error) { ctx := context.Background() var cursor uint64 var keys []string var err error for { var result []string result, cursor, err = client.Scan(ctx, cursor, prefix+"*", 1000).Result() if err != nil { return nil, err } keys = append(keys, result...) if cursor == 0 { break } } return keys, nil } // 删除指定的键 // 谨慎打开删除动作 func deleteKeys(client *redis.Client, keys []string) error { ctx := context.Background() for index, key := range keys { // 打开del操作 err := client.Del(ctx, key).Err() if err != nil { return err } log.Printf("key:%v,index:%v\n", key, index) } return nil }