In Redis, you can use the “HMSET” command to store a dictionary as a hash.
For example, if you have a dictionary called “mydict” and want to store it in Redis with the key “mykey”, you can use the following command:
HMSET mykey field1 value1 field2 value2 ...
To retrieve the dictionary from Redis, you can use the “HGETALL” command. For example:
HGETALL mykey
This will return all the fields and values in the hash as a dictionary.
You can also use HMSET
and HGETALL
in redis-py, which is the python client library for Redis.
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.hmset("mykey", {"field1": "value1", "field2": "value2"})
r.hgetall("mykey")