Skip to content

Commit

Permalink
feat: implement EXPIRETIME and PEXPIRETIME
Browse files Browse the repository at this point in the history
fix #323, #324
  • Loading branch information
cunla committed Sep 22, 2024
1 parent dcd0cf9 commit 11c986f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 2 additions & 3 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ description: Change log of all fakeredis releases

- Implement support for hash expiration related commands @j00bar #328
- `HEXPIRE`, `HEXPIREAT`, `HEXPIRETIME`, `HPERSIST`, `HPEXPIRE`, `HPEXPIREAT`, `HPEXPIRETIME`, `HPTTL`, `HTTL`,
- Implement support for `SORT_RO` #325
-
- Implement support for `SORT_RO` #325, `EXPIRETIME` #323, and `PEXPIRETIME` #324

## v2.24.1

### 🐛 Bug Fixes
Expand Down
20 changes: 18 additions & 2 deletions fakeredis/commands_mixins/generic_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,26 @@ def pexpire(self, key, ms):
def pexpireat(self, key, ms_timestamp):
return self._expireat(key, ms_timestamp / 1000.0)

@command((Key(),))
def pttl(self, key):
@command(name="PTTL", fixed=(Key(),))
def pttl(self, key: CommandItem) -> int:
return self._ttl(key, 1000.0)

@command(name="EXPIRETIME", fixed=(Key(),))
def expiretime(self, key: CommandItem) -> int:
if key.value is None:
return -2
if key.expireat is None:
return -1
return key.expireat

@command(name="PEXPIRETIME", fixed=(Key(),))
def pexpiretime(self, key: CommandItem) -> int:
if key.value is None:
return -2
if key.expireat is None:
return -1
return key.expireat * 1000

@command(())
def randomkey(self):
keys = list(self._db.keys())
Expand Down
14 changes: 14 additions & 0 deletions test/test_mixins/test_generic_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ def test_sort_ro(r: redis.Redis):
assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"]


@pytest.mark.min_server("7")
def test_expiretime(r: redis.Redis):
r.set("a", "foo")
r.expireat("a", 33177117420)
assert r.expiretime("a") == 33177117420


@pytest.mark.min_server("7")
def test_pexpiretime(r: redis.Redis):
r.set("a", "foo")
r.pexpireat("a", 33177117420000)
assert r.pexpiretime("a") == 33177117420000


def test_sort_empty(r: redis.Redis):
assert r.sort("foo") == []

Expand Down

0 comments on commit 11c986f

Please sign in to comment.