Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis: Return all values stored in a database

Tags:

python

redis

We're using Redis to store various application configurations in a DB 0.

Is it possible to query Redis for every key/valuie pair within the database, without having to perform two separate queries and joining the key/value pairs yourself?

I would expect functionality similar to the following:

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

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

However after scouring StackOverflow, Google and Redis documentation, the only solution I can derive (I haven't yet found anyone else asking this question..) is something similar to 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, in assuming there to be a more elegant approach to this problem?

Additional Info

Every value within this database is a String.

My question is not how to retrieve values for each unique data type or data-type related at all.

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

like image 554
Joshua Burns Avatar asked Oct 09 '13 20:10

Joshua Burns


People also ask

How do I get all Redis values?

Redis GET all Keys To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

Which command is used to obtain all the keys in a database Redis?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).

How do I view Redis data?

A Redis server has 16 databases by default. You can check the actual number by running redis-cli config get databases. In interactive mode, the database number is displayed in the prompt within square braces. For example, 127.0. 0.1:6379[13] shows that the 13th database is in use.

What returns an array of values in Redis?

When MGET is used, Redis returns an array of values.


1 Answers

There are differences between different types in Redis, so you have 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 == "string":
        val = redis.get(key)
    if type == "hash":
        vals = redis.hgetall(key)
    if type == "zset":
        vals = redis.zrange(key, 0, -1)
    if type == "list":
        vals = redis.lrange(key, 0, -1)
    if type == "set":
        vals = redis. smembers(key)
like image 181
ideawu Avatar answered Oct 22 '22 07:10

ideawu