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)
Comparing the hset vs hmset in docs isn't clear to me.
The only difference between the commands HSET and HMSET is the return value of the commands. HSET return value (Integer reply): # if the field is a new field in the hash and value was set. (where # is the number of new fields created )
Redis HSET command is used to set field in the hash stored at the key to value. If the key does not exist, a new key holding a hash is created. If the field already exists in the hash, it is overwritten.
Redis HMSET command is used to set the specified fields to their respective values in the hash stored at the key. This command overwrites any existing fields in the hash. If the key does not exist, a new key holding a hash is created.
The command returns an integer value indicating the number of fields removed from the hash. If the field does not exist, the command ignores it and only removes the existing ones. To check if a field exists in the hash, use the HEXISTS command. The command returns integer 1 if the key exists and 0 if not.
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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With