How to store and retrieve a dictionary with redis

前端 未结 11 1304
遇见更好的自我
遇见更好的自我 2020-12-07 10:20
# I have the dictionary my_dict
my_dict = {
    \'var1\' : 5
    \'var2\' : 9
}
r = redis.StrictRedis()

How would I store my_dict and retrieve it w

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 10:45

    You can do it by hmset (multiple keys can be set using hmset).

    hmset("RedisKey", dictionaryToSet)

    import redis
    conn = 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'}
    

提交回复
热议问题