redis scan命令

使用redis的scan命令组,scan、hscan、sscan、zscan在批量操作redis key时可以避免阻塞redis

scan命令

准备数据

not connected> set scana aaaa
OK
127.0.0.1:6379> set scanb bbbb
OK
127.0.0.1:6379> set scanc cccc
OK
127.0.0.1:6379> set scand dddd
OK
127.0.0.1:6379> set scane eeee
OK
127.0.0.1:6379> set scanf ffff
OK
127.0.0.1:6379> set scang gggg
OK
127.0.0.1:6379> set scanh hhhh
OK
127.0.0.1:6379> set scani iiii
OK
127.0.0.1:6379> set scanj jjjj
OK
127.0.0.1:6379> set scank kkkk

使用

第一次scan ,游标为0

scan 0 match 'scan*' count 2

返回

1) "12"
2) 1) "scang"

第二次scan,游标为上次结果返回的最后一个id

scan 12 match 'scan*' count 2

返回

1) "10"
2) 1) "scank"
   2) "scani"

最后一次返回

1) "0"
2) (empty array)

hscan命令

准备数据

127.0.0.1:6379> hset hscan2 scan1 11111
(integer) 1
127.0.0.1:6379> hset hscan2 scan2 22222
(integer) 1
127.0.0.1:6379> hset hscan2 bbbbb 3333
(integer) 1
127.0.0.1:6379> hset hscan2 scan3 44444
(integer) 1
127.0.0.1:6379> hset hscan2 scan4 555555
(integer) 1
127.0.0.1:6379> hset hscan2 scan5 sdfsdf
(integer) 1
127.0.0.1:6379> hset hscan2 scan6 dsfsdf

执行hscan

hscan hscan2 0 match 'scan*' COUNT 2

返回

1) "0"
2)  1) "scan1"
    2) "11111"
    3) "scan2"
    4) "22222"
    5) "scan3"
    6) "44444"
    7) "scan4"
    8) "555555"
    9) "scan5"
   10) "sdfsdf"
   11) "scan6"
   12) "dsfsdf"

sscan命令

准备数据

127.0.0.1:6379> sadd hset2 scanaaaa
(integer) 1
127.0.0.1:6379> sadd hset2 scanbbbb
(integer) 1
127.0.0.1:6379> sadd hset2 scancccc
(integer) 1
127.0.0.1:6379> sadd hset2 ddddd
(integer) 1
127.0.0.1:6379> sadd hset2 scaneeeee
(integer) 1

第一次sscan

sscan hset2 0 match 'scan*' COUNT 2

返回

1) "3"
2) 1) "scanbbbb"
   2) "scaneeeee"

最后一次scan

 sscan hset2 3 match 'scan*' COUNT 2
1) "0"
2) 1) "scancccc"
   2) "scanaaaa"

zscan命令

准备数据

127.0.0.1:6379> zadd zset2 12 scan23
Error: Broken pipe
not connected> zadd zset2 12 scan23
(integer) 1
127.0.0.1:6379> zadd zset2 45 scan45
(integer) 1
127.0.0.1:6379> zadd zset2 57 scan57
(integer) 1
127.0.0.1:6379> zadd zset2 23 sdfdsds
(integer) 1

执行zscan

zscan zset2 0 match "scan*" count 2

你可能感兴趣的:(redis,redis,数据库,缓存,redis,scan)