我在我的一个 python 项目中使用 redis 2.8.4。在特定的排序集中,我想zadd
使用该nx
选项。redis-pynx
文档中没有记录该选项,并且根据文档,它仅适用于 redis 3.0.2 或更高版本。
那么我可以遵循什么替代模式来模拟nx
选项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)