Skip to content
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

Add support for KEEPTTL option of SET command #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion aredis/commands/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,16 @@ async def psetex(self, name, time_ms, value):
time_ms = (time_ms.seconds + time_ms.days * 24 * 3600) * 1000 + ms
return await self.execute_command('PSETEX', name, time_ms, value)

async def set(self, name, value, ex=None, px=None, nx=False, xx=False):
async def set(self, name, value, ex=None, px=None, keepttl=False, nx=False, xx=False):
"""
Set the value at key ``name`` to ``value``

``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.

``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.

``keepttl`` if True, retain the time to live associated with the key.

``nx`` if set to True, set the value at key ``name`` to ``value`` if it
does not already exist.

Expand All @@ -262,6 +264,8 @@ async def set(self, name, value, ex=None, px=None, nx=False, xx=False):
ms = int(px.microseconds / 1000)
px = (px.seconds + px.days * 24 * 3600) * 1000 + ms
pieces.append(px)
if keepttl:
pieces.append('KEEPTTL')

if nx:
pieces.append('NX')
Expand Down
11 changes: 11 additions & 0 deletions tests/client/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,17 @@ async def test_set_multipleoptions(self, r):
assert await r.set('a', '1', xx=True, px=10000)
assert 0 < await r.ttl('a') <= 10

@skip_if_server_version_lt('5.9.0')
@pytest.mark.asyncio(forbid_global_loop=True)
async def test_set_keepttl(self, r):
await r.flushdb()
await r.set('a', 'val')
assert await r.set('a', '1', xx=True, px=10000)
assert 0 < await r.ttl('a') <= 10
await r.set('a', '2', keepttl=True)
assert await r.get('a') == b('2')
assert 0 < await r.ttl('a') <= 10

@pytest.mark.asyncio(forbid_global_loop=True)
async def test_setex(self, r):
await r.flushdb()
Expand Down
11 changes: 11 additions & 0 deletions tests/cluster/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,17 @@ async def test_set_multipleoptions(self, r):
assert await r.set('a', '1', xx=True, px=10000)
assert 0 < await r.ttl('a') <= 10

@pytest.mark.asyncio
@skip_if_server_version_lt('5.9.0')
async def test_set_keepttl(self, r):
await r.flushdb()
await r.set('a', 'val')
assert await r.set('a', '1', xx=True, px=10000)
assert 0 < await r.ttl('a') <= 10
await r.set('a', '2', keepttl=True)
assert await r.get('a') == b('2')
assert 0 < await r.ttl('a') <= 10

@pytest.mark.asyncio
async def test_setex(self, r):
await r.flushdb()
Expand Down