Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paginating "KEYS" command on Redis

Tags:

python

redis

I need all the keys in redis matching a given pattern : *_xyz_*, then I get all their values by following python code:-

  def get_keys(self,pattern):
    self.r_prod.keys(pattern);
    keys = self.r_prod.execute();

    for i in keys[0]:
      self.r_prod.get(i);
    return self.r_prod.execute();

Now keys is pretty huge to hold all in the memory. So, I am wondering is there any way to paginate keys call for a certain limit?

like image 491
Mangat Rai Modi Avatar asked Oct 28 '25 07:10

Mangat Rai Modi


1 Answers

Use SCAN command:

>>> import redis
>>> r = redis.Redis()
>>> for x in r.scan_iter('dummy*'):
...   print(x)
... 
b'dummy3'
b'dummy2'
b'dummy1'
like image 138
alexanderlukanin13 Avatar answered Oct 29 '25 22:10

alexanderlukanin13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!