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

1.9k views Asked by At

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?

2

There are 2 answers

0
Alexey Shrub On BEST ANSWER

I found solution in github issue:

redis.execute_command('ZADD', set_name, 'NX', score, key)
2
Hassan Baig On

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.