Skip to content

Commit febede1

Browse files
authored
Pipeline DISCARD support (#1565)
closes #1539 Part of #1546
1 parent b6ecd0d commit febede1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

redis/client.py

+6
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,12 @@ def execute(self, raise_on_error=True):
18101810
finally:
18111811
self.reset()
18121812

1813+
def discard(self):
1814+
"""Flushes all previously queued commands
1815+
See: https://redis.io/commands/DISCARD
1816+
"""
1817+
self.execute_command("DISCARD")
1818+
18131819
def watch(self, *names):
18141820
"Watches the values at keys ``names``"
18151821
if self.explicit_transaction:

tests/test_pipeline.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
import redis
4-
from .conftest import wait_for_command
4+
from .conftest import wait_for_command, skip_if_server_version_lt
55

66

77
class TestPipeline:
@@ -353,3 +353,29 @@ def test_pipeline_with_bitfield(self, r):
353353

354354
assert pipe == pipe2
355355
assert response == [True, [0, 0, 15, 15, 14], b'1']
356+
357+
@skip_if_server_version_lt('2.0.0')
358+
def test_pipeline_discard(self, r):
359+
360+
# empty pipeline should raise an error
361+
with r.pipeline() as pipe:
362+
pipe.set('key', 'someval')
363+
pipe.discard()
364+
with pytest.raises(redis.exceptions.ResponseError):
365+
pipe.execute()
366+
367+
# setting a pipeline and discarding should do the same
368+
with r.pipeline() as pipe:
369+
pipe.set('key', 'someval')
370+
pipe.set('someotherkey', 'val')
371+
response = pipe.execute()
372+
pipe.set('key', 'another value!')
373+
pipe.discard()
374+
pipe.set('key', 'another vae!')
375+
with pytest.raises(redis.exceptions.ResponseError):
376+
pipe.execute()
377+
378+
pipe.set('foo', 'bar')
379+
response = pipe.execute()
380+
assert response[0]
381+
assert r.get('foo') == b'bar'

0 commit comments

Comments
 (0)