# I have the dictionary my_dict my_dict = { 'var1' : 5 'var2' : 9 } r = redis.StrictRedis()
我将如何存储my_dict并使用Redis进行检索。例如,以下代码不起作用。
#Code that doesn't work r.set('this_dict', my_dict) # to store my_dict in this_dict r.get('this_dict') # to retrieve my_dict
您可以通过hmset(使用来设置多个键hmset)来做到这一点。
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'}