简体   繁体   中英

Using nx option when doing zadd in redis-py (redis ver 2.8.4)

I'm using redis 2.8.4 in a python project of mine. In a particular sorted set, I want to zadd with the nx option. The nx option isn't documented in redis-py docs, and according to the docs , it only works with redis 3.0.2 or greater.

So what's an alternative pattern I can follow to simulate the nx option when doing zadd ?

The most efficient pattern that comes to my mind is using using zscore (or zrank ) to test for existence. If value exists, do nothing, otherwise, zadd the relevant value.

我在github问题中找到了解决方案:

redis.execute_command('ZADD', set_name, 'NX', score, key)

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