Skip to content

Commit

Permalink
fix:add support for exat/pxat (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla authored Jan 30, 2024
1 parent f05337b commit 4393c48
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
1 change: 1 addition & 0 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ description: Change log of all fakeredis releases
### 🐛 Bug Fixes

- Fix XREAD blocking bug #274 #275
- EXAT option does not work #279

### 🧰 Maintenance

Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mkdocs==1.5.3
mkdocs-material==9.5.4
mkdocs-material==9.5.6
14 changes: 11 additions & 3 deletions fakeredis/commands_mixins/string_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,19 @@ def psetex(self, key, ms, value):

@command(name="SET", fixed=(Key(), bytes), repeat=(bytes,))
def set_(self, key, value, *args):
(ex, px, xx, nx, keepttl, get), _ = extract_args(
args, ("+ex", "+px", "xx", "nx", "keepttl", "get")
(ex, px, exat, pxat, xx, nx, keepttl, get), _ = extract_args(
args, ("+ex", "+px", "+exat", "+pxat", "xx", "nx", "keepttl", "get")
)
if ex is not None and (ex <= 0 or (self._db.time + ex) * 1000 >= 2 ** 63):
raise SimpleError(msgs.INVALID_EXPIRE_MSG.format("set"))
if px is not None and (px <= 0 or self._db.time * 1000 + px >= 2 ** 63):
raise SimpleError(msgs.INVALID_EXPIRE_MSG.format("set"))
if exat is not None and (exat <= 0 or exat * 1000 >= 2 ** 63):
raise SimpleError(msgs.INVALID_EXPIRE_MSG.format("set"))
if pxat is not None and (pxat <= 0 or pxat >= 2 ** 63):
raise SimpleError(msgs.INVALID_EXPIRE_MSG.format("set"))

if (xx and nx) or ((px is not None) + (ex is not None) + keepttl > 1):
if (xx and nx) or (sum(x is not None for x in [ex, px, exat, pxat]) + keepttl > 1):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
if nx and get and self.version < (7,):
# The command docs say this is allowed from Redis 7.0.
Expand All @@ -193,6 +197,10 @@ def set_(self, key, value, *args):
key.value = value
else:
key.update(value)
if exat is not None:
key.expireat = exat
if pxat is not None:
key.expireat = pxat / 1000.0
if ex is not None:
key.expireat = self._db.time + ex
if px is not None:
Expand Down
66 changes: 33 additions & 33 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/test_mixins/test_string_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ def test_set_ex(r: redis.Redis):
assert r.get('foo') == b'bar'


@pytest.mark.min_server('6.2')
def test_set_exat(r: redis.Redis):
curr_time = int(time.time())
assert r.set('foo', 'bar', exat=curr_time + 100) is True
assert r.get('foo') == b'bar'


@pytest.mark.min_server('6.2')
def test_set_pxat(r: redis.Redis):
curr_time = int(time.time() * 1000)
assert r.set('foo', 'bar', pxat=curr_time + 100) is True
assert r.get('foo') == b'bar'
time.sleep(0.1)
assert r.get('foo') is None


def test_set_ex_using_timedelta(r: redis.Redis):
assert r.set('foo', 'bar', ex=timedelta(seconds=100)) is True
assert r.get('foo') == b'bar'
Expand Down

0 comments on commit 4393c48

Please sign in to comment.