close

How to store and retrieve a dictionary with redis

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to store and retrieve a dictionary with redis in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to store and retrieve a dictionary with redis?

  1. 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)

  2. store and retrieve a dictionary with redis

    You can do it by hmset (multiple keys can be set using hmset).
    hmset("RedisKey", dictionaryToSet)

Method 1

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'}

Method 2

you can pickle your dict and save as string.

import pickle
import redis

r = redis.StrictRedis('localhost')
mydict = {1:2,2:3,3:4}
p_mydict = pickle.dumps(mydict)
r.set('mydict',p_mydict)

read_dict = r.get('mydict')
yourdict = pickle.loads(read_dict)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read