Skip to content

Commit

Permalink
Python: add RANDOMKEY command
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-congo committed Jun 27, 2024
1 parent 187b2ba commit d90a43d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* Python: Added FLUSHDB command ([#1680](https://github.com/aws/glide-for-redis/pull/1680))
* Python: Added XGROUP SETID command ([#1683](https://github.com/aws/glide-for-redis/pull/1683))
* Python: Added FUNCTION LOAD command ([#1699](https://github.com/aws/glide-for-redis/pull/1699))
* Python: Added RANDOMKEY command ([#1701](https://github.com/aws/glide-for-redis/pull/1701))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))
Expand Down
2 changes: 1 addition & 1 deletion glide-core/src/client/standalone_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl StandaloneClient {
Some(ResponsePolicy::OneSucceeded) => future::select_ok(requests.map(Box::pin))
.await
.map(|(result, _)| result),
Some(ResponsePolicy::OneSucceededNonEmpty) => {
Some(ResponsePolicy::FirstSucceededNonEmptyOrAllEmpty) => {
future::select_ok(requests.map(|request| {
Box::pin(async move {
let result = request.await?;
Expand Down
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 @@ -658,3 +658,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 existing key name.
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 existing key name.
Examples:
>>> await client.random_key()
"random_key_name" # "random_key_name" is a random existing key name.
"""
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 @@ -595,3 +595,21 @@ async def lolwut(
str,
await self._execute_command(RequestType.Lolwut, args),
)

async def random_key(self) -> Optional[str]:
"""
Returns a random existing key name from the currently selected database.
See https://valkey.io/commands/randomkey for more details.
Returns:
Optional[str]: A random existing key name from the currently selected database.
Examples:
>>> await client.random_key()
"random_key_name" # "random_key_name" is a random existing key name from the currently selected 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 @@ -3789,6 +3789,17 @@ def lolwut(
args.extend(str(var))
return self.append_command(RequestType.Lolwut, args)

def random_key(self: TTransaction) -> TTransaction:
"""
Returns a random existing key name.
See https://valkey.io/commands/randomkey for more details.
Command response:
Optional[str]: A random existing key name.
"""
return self.append_command(RequestType.RandomKey, [])


class Transaction(BaseTransaction):
"""
Expand Down
39 changes: 39 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6668,6 +6668,45 @@ 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])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_cluster_client_random_key(self, redis_client: GlideClusterClient):
key = get_random_string(10)

# setup: delete all keys
assert await redis_client.flushall(FlushMode.SYNC)

# no keys exists, so random_key returns None
assert await redis_client.random_key() is None

assert await redis_client.set(key, "foo") == OK
# `key` should be the only existing key, so random_key should return `key`
assert await redis_client.random_key() == key
assert await redis_client.random_key(AllPrimaries()) == key

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

# setup: delete all keys in DB 0 and DB 1
assert await redis_client.select(0) == OK
assert await redis_client.flushdb(FlushMode.SYNC) == OK
assert await redis_client.select(1) == OK
assert await redis_client.flushdb(FlushMode.SYNC) == OK

# no keys exist so random_key returns None
assert await redis_client.random_key() is None
# set `key` in DB 1
assert await redis_client.set(key, "foo") == OK
# `key` should be the only key in the database
assert await redis_client.random_key() == key

# switch back to DB 0
assert await redis_client.select(0) == OK
# DB 0 should still have no keys, so random_key should still return None
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 @@ -563,6 +563,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

0 comments on commit d90a43d

Please sign in to comment.