golang-redis scanning 的操作
本文来自于 github.com/go-redis/redis/v9 的自带的测试代码 commands_test
2、scanning
1、Scan(ctx context.Context, cursor uint64, match string, count int64) 查询 key
ctx := context.Background()
InitRedis()
for i := 0; i < 1000; i++ {
RDB.Set(ctx, fmt.Sprintf("key%d", i), fmt.Sprintf("hello+%d", i), 0)
}
// 参数 ctx
// 参数 cursor 游标
// 参数 match 匹配的模式
// 参数 count 指定每次遍历多少个集合
result, cursor, _ := RDB.Scan(ctx, 0, "key56*", 1000).Result()
fmt.Println(result)
fmt.Println(cursor)
2、ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) 查询 key
// 多了一个类型参数 【string, list , set, zset, hash】
func TestScanType(t *testing.T) { ctx := context.Background() InitRedis() // 参数 ctx // 参数 cursor 游标 // 参数 match 匹配的模式 // 参数 count 指定每次遍历多少个集合 // 参数 keyType result, cursor, _ := RDB.ScanType(ctx, 0, "key12*", 1000, "string").Result() fmt.Println(result) fmt.Println(cursor) }
3、SScan(ctx context.Context, key string, cursor uint64, match string, count int64)
查找一个 set 集合的 key
// 查找一个 set 集合的 key func TestSScan(t *testing.T) { ctx := context.Background() InitRedis() for i := 0; i < 1000; i++ { RDB.SAdd(ctx, "myset", fmt.Sprintf("member%d", i)) }
result, cursor, _ := RDB.SScan(ctx, "myset", 0, "member12*", 1000).Result()
fmt.Println(result)
fmt.Println(cursor)
}
4、HScan(ctx context.Context, key string, cursor uint64, match string, count int64)
查找一个 Hash 集合中的key
func TestHScan(t *testing.T) { ctx := context.Background() InitRedis() for i := 0; i < 1000; i++ { RDB.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") } result, cursor, _ := RDB.HScan(ctx, "myhash", 0, "key12*", 1000).Result() fmt.Println(result) fmt.Println(cursor) }
5、ZScan(ctx context.Context, key string, cursor uint64, match string, count int64)
查找一个 Zset 集合中的 key
func TestZScan(t *testing.T) { ctx := context.Background() InitRedis() for i := 0; i < 1000; i++ { RDB.ZAdd(ctx, "z_myset", redis.Z{ Score: float64(i), Member: fmt.Sprintf("member%d", i), // key }) } result, cursor, _ := RDB.ZScan(ctx, "z_myset", 0, "member12*", 1000).Result() fmt.Println(result) fmt.Println(cursor) }

浙公网安备 33010602011771号