【发布时间】:2019-12-12 22:50:44
【问题描述】:
我正在使用 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 是否会扫描扫描操作开始后插入的数据?如果没有,还有其他方法可以实现吗?
提前致谢!!
【问题讨论】:
标签: redis amazon-elasticache redis-cluster