Skip to content

Commit

Permalink
- added replace option to restore command
Browse files Browse the repository at this point in the history
  • Loading branch information
mumumu committed Dec 27, 2015
1 parent da13784 commit 6e6c3bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,12 +1031,15 @@ def renamenx(self, src, dst):
"Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"
return self.execute_command('RENAMENX', src, dst)

def restore(self, name, ttl, value):
def restore(self, name, ttl, value, replace=False):
"""
Create a key using the provided serialized value, previously obtained
using DUMP.
"""
return self.execute_command('RESTORE', name, ttl, value)
params = [name, ttl, value]
if replace:
params.append('REPLACE')
return self.execute_command('RESTORE', *params)

def set(self, name, value, ex=None, px=None, nx=False, xx=False):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ def test_dump_and_restore(self, r):
r.restore('a', 0, dumped)
assert r['a'] == b('foo')

@skip_if_server_version_lt('3.0.0')
def test_dump_and_restore_and_replace(self, r):
r['a'] = 'bar'
dumped = r.dump('a')
with pytest.raises(redis.ResponseError):
r.restore('a', 0, dumped)

r.restore('a', 0, dumped, replace=True)
assert r['a'] == b('bar')

def test_exists(self, r):
assert not r.exists('a')
r['a'] = 'foo'
Expand Down

0 comments on commit 6e6c3bd

Please sign in to comment.