Redis: returns all values ​​stored in the database

We use Redis to store various application configurations in DB 0.

Is it possible to query Redis for each key / valuie pair in the database, without having to do two separate queries and independently attach the key / value pairs?

I would expect functionality similar to the following:

kv = redis_conn.getall() # --OR-- # kv = redis_conn.mget('*') 

... Where kv will return a tuple of tuples, a list of lists, or a dictionary:

However, after clearing the documentation of StackOverflow, Google, and Redis, the only solution I can get (I have not yet found that someone else is asking this question ..) seems like the following:

 import redis red = redis.Redis(host='localhost', db=0) keys = red.keys() vals = red.mget(keys) kv = zip(keys, vals) 

Am I crazy here, suggesting there will be a more elegant approach for this problem?

Additional Information

Each value in this database is a string.

My question is not how to get the values ​​for each unique data type or data type in general.

Rather, my question is: is there a way to say "hey Redis, return to me every string value in the database" without having to request keys, then request values ​​based on the returned keys?

+8
python redis
source share
1 answer

There are differences between different types in Redis, so you need to look at the data type to determine how to get the values ​​from the key. So:

 keys = redis.keys('*') for key in keys: type = redis.type(key) if type == KV: val = redis.get(key) if type == HASH: vals = redis.hgetall(key) if type == ZSET: vals = redis.zrange(key, 0, -1) 
+20
source share

All Articles