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

Python: adds TOUCH command #1582

Merged
merged 1 commit into from
Jun 18, 2024
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
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3


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
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved

@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
Loading