gpt4 book ai didi

redis - redis scan 是否会扫描开始扫描操作后插入的数据?

转载 作者:可可西里 更新时间:2023-11-01 11:14:14 34 4
gpt4 key购买 nike

我正在使用 redis 扫描来迭代我的 redis 数据库中的键。这是我正在使用的代码

#example :1

start_cursor=0

r=redis.StrictRedis(host='localhost',port=6379)
cur, keys = r.scan(cursor=start_cursor, match='*', count=3)
records=len(keys)
values=[]
values.extend(i for i in keys)
print cur,records,values


while cur != 0:
cur, keys = r.scan(cursor=cur, match='*', count=3)
records+=len(keys)
data = r.mget(keys=keys)
values.extend(i for i in keys)
print cur, len(keys),values

print "Total records scanned :{}".format(records)

我的数据库具有以下格式的值 (key,values) (1,1)(2,2)(3,3)(4,4)(5,5)

当我用

扫描我的记录时

start_cursor=0

我得到以下记录

scan #=>Cursor,new records,all records

scan #1 =>5,3,['1', '2', '5']
scan #2 =>0,2,['1', '2', '5', '3', '4']
Total Records scanned :5

当我使用扫描 #1 中的光标编号开始扫描时,说 (5)。在我的示例 1 中,我从扫描 #2 中获取记录

Example : 2
start_cursor=5
scan #=>Cursor,new records,all records

scan #1 =>0 2 ['3', '4']
Total Records scanned :2

到目前为止一切都很好。当我再插入四个新数据时说 (6,6) (7,7) (8,8) (9,9) 并使用 start_cursor = 5 开始扫描数据 与示例相同:2我遗漏了一些数据,比如这里遗漏了 (6)。

Example : 3
start_cursor=5
scan #=>Cursor,new records,all records

scan #1 =>3 3 ['3', '4', '9']
scan #1 =>0 2 ['3', '4', '9', '7', '8']
Total Records scanned :5

redis scan是否会扫描开始后插入的数据?如果没有,还有其他方法可以实现吗?

提前致谢!

最佳答案

来自doc :

Elements that were not constantly present in the collection during a full iteration, may be returned or not: it is undefined.

由于数据是在扫描操作开始后插入的,因此这些数据并非始终存在。所以 SCAN 不保证返回新插入的数据。

If not is there is any other way to achieve this?

据我所知,没有其他方法可以做到这一点。

关于redis - redis scan 是否会扫描开始扫描操作后插入的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57358164/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com