How to store and retrieve a dictionary with redis
You can do it by hmset
(multiple keys can be set using hmset
).
hmset("RedisKey", dictionaryToSet)
import redisconn = redis.Redis('localhost')user = {"Name":"Pradeep", "Company":"SCTL", "Address":"Mumbai", "Location":"RCP"}conn.hmset("pythonDict", user)conn.hgetall("pythonDict"){'Company': 'SCTL', 'Address': 'Mumbai', 'Location': 'RCP', 'Name': 'Pradeep'}
Another way: you can use RedisWorks
library.
pip install redisworks
>>> from redisworks import Root>>> root = Root()>>> root.something = {1:"a", "b": {2: 2}} # saves it as Hash type in Redis...>>> print(root.something) # loads it from Redis{'b': {2: 2}, 1: 'a'}>>> root.something['b'][2]2
It converts python types to Redis types and vice-versa.
>>> root.sides = [10, [1, 2]] # saves it as list in Redis.>>> print(root.sides) # loads it from Redis[10, [1, 2]]>>> type(root.sides[1])<class 'list'>
Disclaimer: I wrote the library. Here is the code: https://github.com/seperman/redisworks