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

1
2
3
4
5
6
7
8
9
10
11
12
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)
}
复制代码

 

posted @   dogRuning  阅读(686)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示