Skip to content

Commit

Permalink
Python: adds PERSIST command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Mar 17, 2024
1 parent 03a562a commit 1265076
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Python: Added ZRANGE command ([#906](https://github.com/aws/glide-for-redis/pull/906))
* Python, Node: Added PTTL command ([#1036](https://github.com/aws/glide-for-redis/pull/1036), [#1082](https://github.com/aws/glide-for-redis/pull/1082))
* Node: Added HVAL command ([#1022](https://github.com/aws/glide-for-redis/pull/1022))
* Node: Added PERSIST command ([#1023](https://github.com/aws/glide-for-redis/pull/1023))
* Python, Node: Added PERSIST command ([#1129](https://github.com/aws/glide-for-redis/pull/1129)), ([#1023](https://github.com/aws/glide-for-redis/pull/1023))
* Node: Added ZREMRANGEBYSCORE command ([#926](https://github.com/aws/glide-for-redis/pull/926))
* Node: Added ZREMRANGEBYRANK command ([#924](https://github.com/aws/glide-for-redis/pull/924))
* Node: Added Xadd, Xtrim commands. ([#1057](https://github.com/aws/glide-for-redis/pull/1057))
Expand Down
25 changes: 25 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,31 @@ async def pttl(
await self._execute_command(RequestType.PTTL, [key]),
)

async def persist(
self,
key: str,
) -> bool:
"""
Remove the existing timeout on `key`, turning the key from volatile (a key with an expire set) to
persistent (a key that will never expire as no timeout is associated).
See https://redis.io/commands/persist/ for more details.
Args:
key (str): TThe key to remove the existing timeout on.
Returns:
bool: False if `key` does not exist or does not have an associated timeout, True if the timeout has been removed.
Examples:
>>> await client.persist("my_key")
True # Indicates that the timeout associated with the key "my_key" was successfully removed.
"""
return cast(
bool,
await self._execute_command(RequestType.Persist, [key]),
)

async def echo(self, message: str) -> str:
"""
Echoes the provided `message` back.
Expand Down
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,24 @@ def pttl(
"""
return self.append_command(RequestType.PTTL, [key])

def persist(
self: TTransaction,
key: str,
) -> TTransaction:
"""
Remove the existing timeout on `key`, turning the key from volatile (a key with an expire set) to
persistent (a key that will never expire as no timeout is associated).
See https://redis.io/commands/persist/ for more details.
Args:
key (str): TThe key to remove the existing timeout on.
Commands response:
bool: False if `key` does not exist or does not have an associated timeout, True if the timeout has been removed.
"""
return self.append_command(RequestType.Persist, [key])

def echo(self: TTransaction, message: str) -> TTransaction:
"""
Echoes the provided `message` back.
Expand Down
10 changes: 10 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,16 @@ async def test_pttl(self, redis_client: TRedisClient):
assert await redis_client.pexpireat(key, current_time * 1000 + 30000)
assert 0 < await redis_client.pttl(key) <= 30000

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_persist(self, redis_client: TRedisClient):
key = get_random_string(10)
assert await redis_client.set(key, "value") == OK
assert not await redis_client.persist(key)

assert await redis_client.expire(key, 10)
assert await redis_client.persist(key)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_zadd_zaddincr(self, redis_client: TRedisClient):
Expand Down
3 changes: 3 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def transaction_test(
transaction.echo(value)
args.append(value)

transaction.persist(key)
args.append(False)

transaction.exists([key])
args.append(1)

Expand Down

0 comments on commit 1265076

Please sign in to comment.