skip to Main Content

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.

4

Answers


  1. You may execute multiple hset for each field/value pair in hmset.

    r.hset('myKey', 'users', 10)
    r.hset('myKey', 'timestamp', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
    r.hset('myKey', 'yet-another-field', 'yet-another-value')
    
    • first parameter is the key name
    • second parameter is the field name
    • third parameter is the value of the field.
    Login or Signup to reply.
  2. 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?

    Login or Signup to reply.
  3. 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)
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search