Skip to content

Commit c225fd3

Browse files
committed
Pipeline DISCARD support
closes redis#1539 Part of redis#1546
1 parent 879584b commit c225fd3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: redis/client.py

+6
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,12 @@ def execute(self, raise_on_error=True):
18021802
finally:
18031803
self.reset()
18041804

1805+
def discard(self):
1806+
"""Flushes all previously queued commands
1807+
See: https://redis.io/commands/DISCARD
1808+
"""
1809+
self.execute_command("DISCARD")
1810+
18051811
def watch(self, *names):
18061812
"Watches the values at keys ``names``"
18071813
if self.explicit_transaction:

Diff for: 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)