简体   繁体   中英

Increment value of a hash field in Redis using Python

I have created a hash in redis in python as follows:

r.hmset('list:123', {'name': 'john', 'count': 5})

How can I increment the value of count for the key list:123 ?

hash = 'list:123'
key = 'count'
n = 1

r.hincrby(hash, key, n)

I haven't tested this, but from the docs it looks like it will do the job.

r.hmset('list:123', {'name': 'john', 'count': 5})
d = r.hgetall('list:123')
d.count += 1
r.hmset('list:123', d)
r.hincrby("list:123", "count", 1)

Use this page for reference

https://redis.io/commands/hincrby

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM