Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch from hmset() to hset() in Redis?

Tags:

python

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)

Comparing the hset vs hmset in docs isn't clear to me.

like image 352
Houman Avatar asked May 15 '20 18:05

Houman


People also ask

What is difference between HSET and Hmset in Redis?

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 )

How does HSET work Redis?

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.

What is Hmset in Redis?

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.

What does Redis HSET return?

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.


Video Answer


2 Answers

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?

like image 161
chash Avatar answered Sep 22 '22 03:09

chash


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)
like image 44
David Cano Avatar answered Sep 22 '22 03:09

David Cano