首页 > 其他 > 详细

redis 游标迭代器

时间:2020-12-29 14:13:21      阅读:47      评论:0      收藏:0      [点我收藏+]

redis 游标迭代器——scan

前言

在 Redis 2.8 之前,我们只能使用 keys 命令来查询我们想要的数据,但这个命令存在两个缺点:

  1. 此命令没有分页功能,我们只能一次性查询出所有符合条件的 key 值,如果查询结果非常巨大,那么得到的输出信息也会非常多;
  2. keys 命令是遍历查询,因此它的查询时间复杂度是 o(n),所以数据量越大查询时间就越长。

然而,比较幸运的是在 Redis 2.8 时推出了 Scan,解决了我们这些问题,下面来看 Scan 的具体使用。

Scan简介

官方对 Scan 命令的描述信息如下。

Scan guarantees

The SCAN command, and the other commands in the SCAN family, are able to provide to the user a set of guarantees associated to full iterations.

- A full iteration always retrieves all the elements that were present in the collection from the start to the end of a full iteration. This means that if a given element is inside the collection when an iteration is started, and is still there when an iteration terminates, then at some point SCANreturned it to the user.
- A full iteration never returns any element that was NOT present in the collection from the start to the end of a full iteration. So if an element was removed before the start of an iteration, and is never added back to the collection for all the time an iteration lasts, SCAN ensures that this element will never be returned.

However because SCAN has very little state associated (just the cursor) it has the following drawbacks:

- A given element may be returned multiple times. It is up to the application to handle the case of duplicated elements, for example only using the returned elements in order to perform operations that are safe when re-applied multiple times.
- Elements that were not constantly present in the collection during a full iteration, may be returned or not: it is undefined.

官方文档地址:

https://redis.io/commands/scan	

翻译为中文的含义是:Scan 及它的相关命令可以保证以下查询规则。

  • 它可以完整返回开始到结束检索集合中出现的所有元素,也就是在整个查询过程中如果这些元素没有被删除,且符合检索条件,则一定会被查询出来;
  • 它可以保证不会查询出,在开始检索之前删除的那些元素。

然后,Scan 命令包含以下缺点:

  • 一个元素可能被返回多次,需要客户端来实现去重;
  • 在迭代过程中如果有元素被修改,那么修改的元素能不能被遍历到不确定。

Scan使用

我们先来看看scan的基本语法:

scan cursor [MATCH pattern] [COUNT count]
  • cursor:光标位置,整数值,从 0 开始,到 0 结束,查询结果是空,但游标值不为 0,表示遍历还没结束;
  • match pattern:正则匹配字段;
  • count:限定服务器单次遍历的字典槽位数量(约等于),只是对增量式迭代命令的一种提示(hint),并不是查询结果返回的最大数量,它的默认值是 10。

接下来我们使用scan命令进行查询;

  1. 我们先使用lua脚本插入十万条数据,脚本如下:
EVAL "for i=1,100000 do redis.call(‘set‘,string.format(KEYS[1],i),string.format(ARGV[1],i)) end" 1 user_token_%d id_%d
  1. 我们来查询用户 id 为 1111* 的数据,Scan 命令使用如下:
127.0.0.1:6379> scan 0 match user_token_1111* count 10000
1) "64408"
2) 1) "user_token_11117"
127.0.0.1:6379> scan 64408 match user_token_1111* count 10000
1) "70348"
2) 1) "user_token_11116"
   2) "user_token_11119"
127.0.0.1:6379> scan 70348 match user_token_1111* count 10000
1) "15154"
2) 1) "user_token_11111"
127.0.0.1:6379> scan 15154 match user_token_1111* count 10000
1) "109670"
2) 1) "user_token_1111"
127.0.0.1:6379> scan 109670 match user_token_1111* count 10000
1) "34814"
2) 1) "user_token_11112"
127.0.0.1:6379> scan 34814 match user_token_1111* count 10000
1) "63129"
2) 1) "user_token_11113"
127.0.0.1:6379> scan 63129 match user_token_1111* count 10000
1) "31949"
2) 1) "user_token_11118"
127.0.0.1:6379> scan 31949 match user_token_1111* count 10000
1) "50995"
2) (empty array)
127.0.0.1:6379> scan 50995 match user_token_1111* count 10000
1) "106087"
2) 1) "user_token_11114"
127.0.0.1:6379> scan 106087 match user_token_1111* count 10000
1) "0"
2) 1) "user_token_11110"
   2) "user_token_11115"

从以上的执行结果,我们看出两个问题:

  1. 查询的结果为空,但游标值不为 0,表示遍历还没结束;
  2. 设置的是 count 10000,但每次返回的数量都不是 10000,且不固定,这是因为 count 只是限定服务器单次遍历的字典槽位数量(约等于),而不是规定返回结果的 count 值。

Scan 相关命令

Scan 是一个系列指令,除了 Scan 之外,还有以下 3 个命令:

  1. HScan 遍历字典游标迭代器
  2. SScan 遍历集合的游标迭代器
  3. ZScan 遍历有序集合的游标迭代器

来看这些命令的具体使用。

HScan 使用

基本语法:

hscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> hscan myhash 0 match k2* count 10
1) "0"
2) 1) "k2"
   2) "v2"
SScan 使用

基本语法:

sscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> sscan myset 0 match v2* count 20
1) "0"
2) 1) "v2"
ZScan 使用

基本语法:

zscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> zscan zset 0 match red* count 20
1) "0"
2) 1) "redis"
   2) "10"

小结

通过本文我们可以知道 Scan 包含以下四个指令:

  1. Scan:用于检索当前数据库中所有数据;
  2. HScan:用于检索哈希类型的数据;
  3. SScan:用于检索集合类型中的数据;
  4. ZScan:由于检索有序集合中的数据。

Scan 具备以下几个特点:

  1. Scan 可以实现 keys 的匹配功能;
  2. Scan 是通过游标进行查询的不会导致 Redis 假死;
  3. Scan 提供了 count 参数,可以规定遍历的数量;
  4. Scan 会把游标返回给客户端,用户客户端继续遍历查询;
  5. Scan 返回的结果可能会有重复数据,需要客户端去重;
  6. 单次返回空值且游标不为 0,说明遍历还没结束;
  7. Scan 可以保证在开始检索之前,被删除的元素一定不会被查询出来;
  8. 在迭代过程中如果有元素被修改, Scan 不保证能查询出相关的元素。

redis 游标迭代器

原文:https://www.cnblogs.com/JerryQTQcjl/p/14205835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!