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

Implement GETEX #102

Merged
merged 8 commits into from
Dec 13, 2022
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
12 changes: 6 additions & 6 deletions REDIS_COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ list of [unimplemented commands](#unimplemented-commands).
Get the value of a key
* [GETDEL](https://redis.io/commands/getdel/)
Get the value of a key and delete the key
* [GETEX](https://redis.io/commands/getex/)
Get the value of a key and optionally set its expiration
* [GETRANGE](https://redis.io/commands/getrange/)
Get a substring of the string stored at a key
* [GETSET](https://redis.io/commands/getset/)
Expand Down Expand Up @@ -730,16 +732,14 @@ All the redis commands are implemented in fakeredis with these exceptions:
* [GEOSEARCHSTORE](https://redis.io/commands/geosearchstore/)
Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle, and store the result in another key.

### string
* [GETEX](https://redis.io/commands/getex/)
Get the value of a key and optionally set its expiration
* [LCS](https://redis.io/commands/lcs/)
Find longest common substring

### hash
* [HRANDFIELD](https://redis.io/commands/hrandfield/)
Get one or multiple random fields from a hash

### string
* [LCS](https://redis.io/commands/lcs/)
Find longest common substring

### hyperloglog
* [PFDEBUG](https://redis.io/commands/pfdebug/)
Internal commands for debugging HyperLogLog values
Expand Down
33 changes: 33 additions & 0 deletions fakeredis/commands_mixins/string_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,36 @@ def strlen(self, key):
@command((Key(bytes), Int, Int))
def substr(self, key, start, end):
return self.getrange(key, start, end)

@command((Key(bytes),), (bytes,))
def getex(self, key, *args):
i, count_options, expire_time, diff = 0, 0, None, None
while i < len(args):
count_options += 1
if casematch(args[i], b'ex') and i + 1 < len(args):
diff = Int.decode(args[i + 1])
expire_time = self._db.time + diff
i += 2
elif casematch(args[i], b'px') and i + 1 < len(args):
diff = Int.decode(args[i + 1])
expire_time = (self._db.time * 1000 + diff) / 1000.0
i += 2
elif casematch(args[i], b'exat') and i + 1 < len(args):
expire_time = Int.decode(args[i + 1])
i += 2
elif casematch(args[i], b'pxat') and i + 1 < len(args):
expire_time = Int.decode(args[i + 1]) / 1000.0
i += 2
elif casematch(args[i], b'persist'):
expire_time = None
i += 1
else:
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
if ((expire_time is not None and (expire_time <= 0 or expire_time * 1000 >= 2 ** 63))
or (diff is not None and (diff <= 0 or diff * 1000 >= 2 ** 63))):
raise SimpleError(msgs.INVALID_EXPIRE_MSG.format('getex'))
if count_options > 1:
raise SimpleError(msgs.SYNTAX_ERROR_MSG)

key.expireat = expire_time
return key.get(None)
7 changes: 4 additions & 3 deletions scripts/create_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def create_issue(self, group: str, cmd: str, summary: str):
link = f"https://redis.io/commands/{cmd.replace(' ', '-')}/"
title = f"Implement support for `{cmd.upper()}` ({group} command)"
filename = f'{group}_mixin.py'
body = f"""Implement support for command `{cmd.upper()}` in {filename}.
body = f"""
Implement support for command `{cmd.upper()}` in {filename}.

{summary}.
{summary}.

Here is the [Official documentation]({link})"""
Here is the [Official documentation]({link})"""
labels = [f'{group}-commands', 'enhancement', 'help wanted']
for label in labels:
if label not in self.labels:
Expand Down
2 changes: 0 additions & 2 deletions test/test_mixins/test_server_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import pytest
from redis.exceptions import ResponseError

from .. import testtools


def test_swapdb(r, create_redis):
r1 = create_redis(1)
Expand Down
36 changes: 36 additions & 0 deletions test/test_mixins/test_string_commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import time
from datetime import timedelta

import pytest
Expand Down Expand Up @@ -476,3 +477,38 @@ def test_setitem_getitem(r):
def test_getitem_non_existent_key(r):
assert r.keys() == []
assert 'noexists' not in r.keys()


@pytest.mark.slow
def test_getex(r: redis.Redis):
# Exceptions
with pytest.raises(redis.ResponseError):
raw_command(r, 'getex', 'foo', 'px', 1000, 'ex', 1)
with pytest.raises(redis.ResponseError):
raw_command(r, 'getex', 'foo', 'dsac', 1000, 'ex', 1)

r.set('foo', 'val')
assert r.getex('foo', ex=1) == b'val'
time.sleep(1.5)
assert r.get('foo') is None

r.set('foo2', 'val')
assert r.getex('foo2', px=1000) == b'val'
time.sleep(1.5)
assert r.get('foo2') is None

r.set('foo4', 'val')
r.getex('foo4', exat=int(time.time() + 1))
time.sleep(1.5)
assert r.get('foo4') is None

r.set('foo2', 'val')
r.getex('foo2', pxat=int(time.time() + 1) * 1000)
time.sleep(1.5)
assert r.get('foo2') is None

r.setex('foo5', 1, 'val')
r.getex('foo5', persist=True)
assert r.ttl('foo5') == -1
time.sleep(1.5)
assert r.get('foo5') == b'val'