'How to switch from hmset() to hset() in Redis?
I get the deprication warning, that Redis.hmset() is deprecated. Use Redis.hset() instead.
However hset() takes a third parameter and I can't figure out what name is supposed to be.
info = {'users': 10, "timestamp": datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')}
r.hmset("myKey", info)
The above works, but this requires a first parameter called name.
r.hset(name, "myKey", info)
Solution 1:[1]
hmset(name, mapping): given a hash name ("myKey") and a dictionary (info) set all key/value pairs.
hset(name, key=None, value=None, mapping=None): given a hash name ("myKey") a key and a value, set the key/value. Alternatively, given a dictionary (mapping=info) set all key/value pairs in mapping.
Source: https://redis-py.readthedocs.io/en/stable/
If this does not work, perhaps you need to update the library?
Solution 2:[2]
The problem is that you must specify within hset() that you are giving it the mapping. In your case:
r.hset("myKey", mapping=info)
instead of
r.hset("myKey", info)
Solution 3:[3]
I was using Redis.hmset() as following:
redis.hmset('myKey', info)
If you use Redis.hset() the following you will not get warning.
redis.hset('myKey', key=None, value=None, mapping=info)
With this usage, we will skip the single key & value adding step and redis.hset() will set all of the key and value mappings in info to myKey.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chash |
| Solution 2 | Tomerikoo |
| Solution 3 | ramazans |
