Skip to content

Commit

Permalink
Python: adds TOUCH command (valkey-io#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon authored and cyip10 committed Jun 24, 2024
1 parent ac77a3d commit 9a6eb0e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Python: Added SETBIT command ([#1571](https://github.com/aws/glide-for-redis/pull/1571))
* Python: Added GETBIT command ([#1575](https://github.com/aws/glide-for-redis/pull/1575))
* Python: Added BITCOUNT command ([#1592](https://github.com/aws/glide-for-redis/pull/1592))
* Python: Added TOUCH command ([#1582](https://github.com/aws/glide-for-redis/pull/1582))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))
Expand Down
23 changes: 23 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,29 @@ async def decrby(self, key: str, amount: int) -> int:
int, await self._execute_command(RequestType.DecrBy, [key, str(amount)])
)

async def touch(self, keys: List[str]) -> int:
"""
Updates the last access time of specified keys.
See https://valkey.io/commands/touch/ for details.
Note:
When in cluster mode, the command may route to multiple nodes when `keys` map to different hash slots.
Args:
keys (List[str]): The keys to update last access time.
Returns:
int: The number of keys that were updated, a key is ignored if it doesn't exist.
Examples:
>>> await client.set("myKey1", "value1")
>>> await client.set("myKey2", "value2")
>>> await client.touch(["myKey1", "myKey2", "nonExistentKey"])
2 # Last access time of 2 keys has been updated.
"""
return cast(int, await self._execute_command(RequestType.Touch, keys))

async def hset(self, key: str, field_value_map: Mapping[str, str]) -> int:
"""
Sets the specified fields to their respective values in the hash stored at `key`.
Expand Down
14 changes: 14 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ def mget(self: TTransaction, keys: List[str]) -> TTransaction:
"""
return self.append_command(RequestType.MGet, keys)

def touch(self: TTransaction, keys: List[str]) -> TTransaction:
"""
Updates the last access time of specified keys.
See https://valkey.io/commands/touch/ for details.
Args:
keys (List[str]): The keys to update last access time.
Commands response:
int: The number of keys that were updated, a key is ignored if it doesn't exist.
"""
return self.append_command(RequestType.Touch, keys)

def config_rewrite(self: TTransaction) -> TTransaction:
"""
Rewrite the configuration file with the current configuration.
Expand Down
14 changes: 13 additions & 1 deletion python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,18 @@ async def test_mset_mget(self, redis_client: TRedisClient):
keys[-1] = None
assert mget_res == keys

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_touch(self, redis_client: TRedisClient):
keys = [get_random_string(10), get_random_string(10)]
key_value_pairs = {key: value for key, value in zip(keys, keys)}

assert await redis_client.mset(key_value_pairs) == OK
assert await redis_client.touch(keys) == 2

# 2 existing keys, one non-existing
assert await redis_client.touch([*keys, get_random_string(3)]) == 2

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_msetnx(self, redis_client: TRedisClient):
Expand Down Expand Up @@ -4942,7 +4954,7 @@ async def test_multi_key_command_routed_to_multiple_nodes(
await redis_client.delete(["abc", "zxy", "lkn"])
await redis_client.mget(["abc", "zxy", "lkn"])
await redis_client.mset({"abc": "1", "zxy": "2", "lkn": "3"})
# TODO touch
await redis_client.touch(["abc", "zxy", "lkn"])


class TestCommandsUnitTests:
Expand Down
2 changes: 2 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async def transaction_test(

transaction.exists([key2])
args.append(1)
transaction.touch([key2])
args.append(1)

transaction.delete([key2])
args.append(1)
Expand Down

0 comments on commit 9a6eb0e

Please sign in to comment.