0

I am using the redis scan to iterate over the key in my redis DB.This is the code i am using

#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)

My DB has following values in the format (key,values) (1,1)(2,2)(3,3)(4,4)(5,5)

When I am scanning my records with


start_cursor=0

I am getting the following records

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

when I am starting the scanning with my cursor number from scan #1 say(5).I am getting the records from the scan #2 in my example 1

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

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

Everything is fine upto now.when I insert four more new data say (6,6) (7,7) (8,8) (9,9) and start the scanning the data with start_cursor = 5 same as example :2 I am missing some of the data say (6) is missed here.

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

Whether redis scan will scan data inserted after scan operation started ? If not is there is any other way to achieve this?

Thanks in advance!!

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34
Jebaseelan Ravi
  • 697
  • 8
  • 22

1 Answers1

2

From the doc:

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

Since the data is inserted after the scan operation starts, these data is NOT CONSTANTLY PRESENT. So SCAN doesn't guarantee to return the newly inserted data.

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

AFAIK, there's no other way to achieve that.

for_stack
  • 21,012
  • 4
  • 35
  • 48