question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for NX/XX/CH/INCR options to ZADD

See original GitHub issue

Redis 3.0.2+ support NX, XX, CH, and INCR options to the ZADD command: http://redis.io/commands/zadd#zadd-options-redis-302-or-greater

It doesn’t seem (from the docs, at least) that redis-py supports these options.

Are you interested in supporting them? Should I bother with a pull request to implement support?

The one API issue is that the use of kwargs in the existing zadd API makes it a bit confusing where to add these options in the API with backward-compatibility. Any thoughts on that?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Granitosauruscommented, Dec 8, 2017

You can monkey patch in this functionality:

import redis
from redis import RedisError


def zadd(self, name, items, nx=False, xx=False):
    if nx and xx:
	raise RedisError("ZADD can't use both NX and XX modes")
    pieces = []
    if nx:
	pieces.append('NX')
    if xx:
	pieces.append('XX')
    for pair in items:
	if len(pair) != 2:
	    raise RedisError("ZADD items have to be pairs")
	pieces.append(pair[1])
	pieces.append(pair[0])
    return self.execute_command('ZADD', name, *pieces)

redis.Redis.zadd = zadd

# then you can use
redis.Redis().zadd('my_key', [('foo', 1), ('bar', 2)], nx=True)

I really think this should be updated even if it breaks backwards compatibility because nx and xx settings are very important in set operations.

7reactions
andymccurdycommented, Sep 1, 2015

Hey Carl, good to hear from you.

Ya, the kwargs issue is why I haven’t already implemented this. There’s a pull request (#638) that implemented each option as a separate command, but that means you can’t mix options together.

My current thought is that the existing API sucks (using kwargs) and instead should just take a dict of items along with separate arguments for these options. This unfortunately breaks exiting code and I’ve been reluctant to bite the bullet.

It’s becoming clearer that it’s time for redis-py 3.0 where I can fix some of these issues without worrying as much about breaking things.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for NX/XX/CH/INCR options to ZADD #649 - GitHub
Redis 3.0.2+ support NX , XX , CH , and INCR options to the ZADD command: http://redis.io/commands/zadd#zadd-options-redis-302-or-greater.
Read more >
ZADD - Redis
ZADD supports a list of options, specified after the name of the key and before the first score argument. Options are: XX: Only...
Read more >
Spring data redis zadd command missed nx|xx|incr options
It seem that the only two zadd methods that DefaultZSetOperations provide can't let me use NX|XX|CH|INCR options: Boolean add(K key, V value, ...
Read more >
How To Manage Sorted Sets in Redis - DigitalOcean
Creating Sorted Sets and Adding Members · NX or XX : These options have opposite effects, so you can only include one of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found