[英]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. 我在我的python项目中使用redis 2.8.4。 In a particular sorted set, I want to
zadd
with the nx
option. 在一个特定的排序集中,我想使用
nx
选项进行zadd
。 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. Redis-py文档中没有记录
nx
选项,并且根据文档 ,它仅适用于Redis 3.0.2或更高版本。
So what's an alternative pattern I can follow to simulate the nx
option when doing zadd
? 那么在执行
zadd
时可以模仿nx
选项的替代模式是什么?
The most efficient pattern that comes to my mind is using using zscore
(or zrank
) to test for existence. 我想到的最有效的模式是使用
zscore
(或zrank
)来测试是否存在。 If value exists, do nothing, otherwise, zadd
the relevant value. 如果值存在,则不执行任何操作,否则,将相关值
zadd
。
我在github问题中找到了解决方案:
redis.execute_command('ZADD', set_name, 'NX', score, key)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.