Skip to content

NOMKSTREAM support for XADD #1507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2434,15 +2434,16 @@ def xack(self, name, groupname, *ids):
"""
return self.execute_command('XACK', name, groupname, *ids)

def xadd(self, name, fields, id='*', maxlen=None, approximate=True):
def xadd(self, name, fields, id='*', maxlen=None, approximate=True,
nomkstream=False):
"""
Add to a stream.
name: name of the stream
fields: dict of field/value pairs to insert into the stream
id: Location to insert this record. By default it is appended.
maxlen: truncate old stream members beyond this size
approximate: actual stream length may be slightly more than maxlen

nomkstream: When set to true, do not make a stream
"""
pieces = []
if maxlen is not None:
Expand All @@ -2452,6 +2453,8 @@ def xadd(self, name, fields, id='*', maxlen=None, approximate=True):
if approximate:
pieces.append(b'~')
pieces.append(str(maxlen))
if nomkstream:
pieces.append(b'NOMKSTREAM')
pieces.append(id)
if not isinstance(fields, dict) or len(fields) == 0:
raise DataError('XADD fields must be a non-empty dict')
Expand Down
10 changes: 10 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,16 @@ def test_xadd(self, r):
r.xadd(stream, {'foo': 'bar'}, maxlen=2, approximate=False)
assert r.xlen(stream) == 2

@skip_if_server_version_lt('6.2.0')
def test_xadd_nomkstream(self, r):
# nomkstream option
stream = 'stream'
r.xadd(stream, {'foo': 'bar'})
r.xadd(stream, {'some': 'other'}, nomkstream=False)
assert r.xlen(stream) == 2
r.xadd(stream, {'some': 'other'}, nomkstream=True)
assert r.xlen(stream) == 3

@skip_if_server_version_lt('5.0.0')
def test_xclaim(self, r):
stream = 'stream'
Expand Down