Skip to content

Add support for the ABSTTL option of the RESTORE command. #1423

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
Nov 22, 2020
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
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@aparcar #1353/#1354
* Added support for the ACL LOG command available in Redis 6. Thanks
@2014BDuck. #1307
* Added support for ABSTTL option of the RESTORE command available in
Redis 5.0. Thanks @charettes. #1423
* 3.5.3 (June 1, 2020)
* Restore try/except clauses to __del__ methods. These will be removed
in 4.0 when more explicit resource management if enforced. #1339
Expand Down
11 changes: 10 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,14 +1811,23 @@ 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, replace=False):
def restore(self, name, ttl, value, replace=False, absttl=False):
"""
Create a key using the provided serialized value, previously obtained
using DUMP.

``replace`` allows an existing key on ``name`` to be overridden. If
it's not specified an error is raised on collision.

``absttl`` if True, specified ``ttl`` should represent an absolute Unix
timestamp in milliseconds in which the key will expire. (Redis 5.0 or
greater).
"""
params = [name, ttl, value]
if replace:
params.append('REPLACE')
if absttl:
params.append('ABSTTL')
return self.execute_command('RESTORE', *params)

def set(self, name, value,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,19 @@ def test_dump_and_restore_and_replace(self, r):
r.restore('a', 0, dumped, replace=True)
assert r['a'] == b'bar'

@skip_if_server_version_lt('5.0.0')
def test_dump_and_restore_absttl(self, r):
r['a'] = 'foo'
dumped = r.dump('a')
del r['a']
ttl = int(
(redis_server_time(r) + datetime.timedelta(minutes=1)).timestamp()
* 1000
)
r.restore('a', ttl, dumped, absttl=True)
assert r['a'] == b'foo'
assert 0 < r.ttl('a') <= 61

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