I can get one Key/Value from Redis with Python in this way:
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
data = r.get('12345')
How to get values from e.g. 2 keys at the same time (with one call)?
I tried with: data = r.get('12345', '54321')
but that does not work..
Also how to get all values based on partial key?
e.g. data = r.get('123*')
You already can retrieve multiple keys, but not arbitrary ones. If your primary goal is to retrieve multiple arbitrary keys, use a hash and hmget. If your primary need is to access a sorted set, use sorted set and either go the scripting route or pipeline a series of zscore calls.
Redis Lists In Redis you can also have lists as the value, which overcomes the problem of having more than one value for a key, as you can have an ordered list with multiple values (so “they'll none of 'em be missed”).
Using Redis-Py package. Create a Python file and add the code shown below to connect to the Redis cluster. Once we have a connection to the server, we can start performing operations. The above example will connect to the database at index 10. The line above will take the first arguments as key and value, respectively.
You can use the method mget
to get the values of several keys in one call (returned in the same order as the keys):
data = r.mget(['123', '456'])
To search for keys following a specific pattern, use the scan
method:
cursor, keys = r.scan(match='123*')
data = r.mget(keys)
(Documentation: https://redis-py.readthedocs.io/en/stable/#)
As @atn says: (and if using django)
from django_redis import get_redis_connection
r = get_redis_connection()
data = r.keys('123*')
works now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With