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: add RANDOMKEY command #396

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,25 @@ async def lolwut(
TClusterResponse[str],
await self._execute_command(RequestType.Lolwut, args, route),
)

async def random_key(self, route: Optional[Route] = None) -> Optional[str]:
"""
Returns a random key from currently selected database.

See https://valkey.io/commands/randomkey for more details.

Args:
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
in which case the client will route the command to the nodes defined by `route`.

Returns:
Optional[str]: A random key from the database.

Examples:
>>> await client.random_key()
"random_key_name" # A random key name that exists in the database.
"""
return cast(
Optional[str],
await self._execute_command(RequestType.RandomKey, [], route),
)
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,21 @@ async def lolwut(
str,
await self._execute_command(RequestType.Lolwut, args),
)

async def random_key(self) -> Optional[str]:
"""
Returns a random key from currently selected database.

See https://valkey.io/commands/randomkey for more details.

Returns:
Optional[str]: A random key from the database.

Examples:
>>> await client.random_key()
"random_key_name" # A random key name that exists in the database.
"""
return cast(
Optional[str],
await self._execute_command(RequestType.RandomKey, []),
)
11 changes: 11 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3737,6 +3737,17 @@ def lolwut(
args.extend(str(var))
return self.append_command(RequestType.Lolwut, args)

def random_key(self: TTransaction) -> TTransaction:
"""
Returns a random key from currently selected database.

See https://valkey.io/commands/randomkey for more details.

Command response:
Optional[str]: A random key from the database.
"""
return self.append_command(RequestType.RandomKey, [])


class Transaction(BaseTransaction):
"""
Expand Down
24 changes: 24 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6487,6 +6487,30 @@ async def test_lolwut(self, redis_client: TGlideClient):
result = await redis_client.lolwut(2, [10, 20], RandomNode())
assert "Redis ver. " in node_result

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_random_key(self, redis_client: TGlideClient):
key = get_random_string(10)

# set a key in the database in case it was empty
assert await redis_client.set(key, "foo") == OK

result = await redis_client.random_key()
assert result is not None
assert await redis_client.exists([result]) == 1

if isinstance(redis_client, GlideClusterClient):
result = await redis_client.random_key(AllPrimaries())
assert result is not None
assert await redis_client.exists([result]) == 1

# TODO: returns a ResponseError for cluster clients but should return None
# Run this code for standalone and cluster clients when this is completed:
# https://github.com/amazon-contributing/redis-rs/pull/153
if isinstance(redis_client, GlideClient):
assert await redis_client.flushall(FlushMode.SYNC)
assert await redis_client.random_key() is None


class TestMultiKeyCommandCrossSlot:
@pytest.mark.parametrize("cluster_mode", [True])
Expand Down
5 changes: 5 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ async def transaction_test(
transaction.flushdb()
args.append(OK)

transaction.set(key, "foo")
args.append(OK)
transaction.random_key()
args.append(key)

min_version = "6.2.0"
if not await check_if_server_version_lt(redis_client, min_version):
transaction.flushall(FlushMode.SYNC)
Expand Down
Loading