Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to redis.keys(...)

Tags:

python

redis

In the documentation, it strongly discourages the use of .keys() in a production environment. What would be an alternative to the following:

r = Redis()
keys = r.keys('RT*')
for key in keys:
    do_something()
like image 839
David542 Avatar asked Mar 20 '15 03:03

David542


People also ask

Is Redis scan blocked?

So yes, SCAN is blocking, but it's usually not a big deal, and is only blocking when Redis is actually processing the command.

Are Keys unique in Redis?

Redis is an open-source, in-memory key-value data store. A key-value data store is a type of NoSQL database in which keys serve as unique identifiers for their associated values. Any given Redis instance includes a number of databases, each of which can hold many different keys of a variety of data types.

Are Redis keys always strings?

Keys in Redis are all strings, so it doesn't really matter what kind of value you pass into a client.


1 Answers

SCAN is the recommended alternative for production usage.

redis-py includes a convenient SCAN iterator for that purpose, so what you can do is:

r = Redis()
for key in r.scan_iter(match='RT*'):
  print(key) # or do something
like image 143
deltheil Avatar answered Oct 11 '22 06:10

deltheil