Skip to content

Commit 1a41cfd

Browse files
authored
Add support for the ABSTTL option of the RESTORE command. (#1423)
Add support for the ABSTTL option of the RESTORE command.
1 parent 8c176cd commit 1a41cfd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@aparcar #1353/#1354
1111
* Added support for the ACL LOG command available in Redis 6. Thanks
1212
@2014BDuck. #1307
13+
* Added support for ABSTTL option of the RESTORE command available in
14+
Redis 5.0. Thanks @charettes. #1423
1315
* 3.5.3 (June 1, 2020)
1416
* Restore try/except clauses to __del__ methods. These will be removed
1517
in 4.0 when more explicit resource management if enforced. #1339

redis/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,14 +1811,23 @@ def renamenx(self, src, dst):
18111811
"Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"
18121812
return self.execute_command('RENAMENX', src, dst)
18131813

1814-
def restore(self, name, ttl, value, replace=False):
1814+
def restore(self, name, ttl, value, replace=False, absttl=False):
18151815
"""
18161816
Create a key using the provided serialized value, previously obtained
18171817
using DUMP.
1818+
1819+
``replace`` allows an existing key on ``name`` to be overridden. If
1820+
it's not specified an error is raised on collision.
1821+
1822+
``absttl`` if True, specified ``ttl`` should represent an absolute Unix
1823+
timestamp in milliseconds in which the key will expire. (Redis 5.0 or
1824+
greater).
18181825
"""
18191826
params = [name, ttl, value]
18201827
if replace:
18211828
params.append('REPLACE')
1829+
if absttl:
1830+
params.append('ABSTTL')
18221831
return self.execute_command('RESTORE', *params)
18231832

18241833
def set(self, name, value,

tests/test_commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,19 @@ def test_dump_and_restore_and_replace(self, r):
642642
r.restore('a', 0, dumped, replace=True)
643643
assert r['a'] == b'bar'
644644

645+
@skip_if_server_version_lt('5.0.0')
646+
def test_dump_and_restore_absttl(self, r):
647+
r['a'] = 'foo'
648+
dumped = r.dump('a')
649+
del r['a']
650+
ttl = int(
651+
(redis_server_time(r) + datetime.timedelta(minutes=1)).timestamp()
652+
* 1000
653+
)
654+
r.restore('a', ttl, dumped, absttl=True)
655+
assert r['a'] == b'foo'
656+
assert 0 < r.ttl('a') <= 61
657+
645658
def test_exists(self, r):
646659
assert r.exists('a') == 0
647660
r['a'] = 'foo'

0 commit comments

Comments
 (0)