Skip to content

Commit

Permalink
Python: add OBJECT REFCOUNT command (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-congo committed May 28, 2024
1 parent 9d5c58a commit f282542
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Python: Added OBJECT ENCODING command ([#1471](https://github.com/aws/glide-for-redis/pull/1471))
* Python: Added OBJECT FREQ command ([#1472](https://github.com/aws/glide-for-redis/pull/1472))
* Python: Added OBJECT IDLETIME command ([#1474](https://github.com/aws/glide-for-redis/pull/1474))
* Python: Added OBJECT REFCOUNT command ([#1485](https://github.com/aws/glide-for-redis/pull/1485))

## 0.4.0 (2024-05-26)

Expand Down
22 changes: 22 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3587,3 +3587,25 @@ async def object_idletime(self, key: str) -> Optional[int]:
Optional[int],
await self._execute_command(RequestType.ObjectIdleTime, [key]),
)

async def object_refcount(self, key: str) -> Optional[int]:
"""
Returns the reference count of the object stored at `key`.
See https://valkey.io/commands/object-refcount for more details.
Args:
key (str): The key of the object to get the reference count of.
Returns:
Optional[int]: If `key` exists, returns the reference count of the object stored at `key` as an integer.
Otherwise, returns None.
Examples:
>>> await client.object_refcount("my_hash")
2 # "my_hash" has a reference count of 2.
"""
return cast(
Optional[int],
await self._execute_command(RequestType.ObjectRefCount, [key]),
)
15 changes: 15 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,21 @@ def object_idletime(self: TTransaction, key: str) -> TTransaction:
"""
return self.append_command(RequestType.ObjectIdleTime, [key])

def object_refcount(self: TTransaction, key: str) -> TTransaction:
"""
Returns the reference count of the object stored at `key`.
See https://valkey.io/commands/object-refcount for more details.
Args:
key (str): The key of the object to get the reference count of.
Command response:
Optional[int]: If `key` exists, returns the reference count of the object stored at `key` as an integer.
Otherwise, returns None.
"""
return self.append_command(RequestType.ObjectRefCount, [key])


class Transaction(BaseTransaction):
"""
Expand Down
11 changes: 11 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,17 @@ async def test_object_idletime(self, redis_client: TRedisClient):
idletime = await redis_client.object_idletime(string_key)
assert idletime is not None and idletime > 0

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_object_refcount(self, redis_client: TRedisClient):
string_key = get_random_string(10)
non_existing_key = get_random_string(10)

assert await redis_client.object_refcount(non_existing_key) is None
assert await redis_client.set(string_key, "foo") == OK
refcount = await redis_client.object_refcount(string_key)
assert refcount is not None and refcount >= 0


class TestMultiKeyCommandCrossSlot:
@pytest.mark.parametrize("cluster_mode", [True])
Expand Down
12 changes: 8 additions & 4 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ async def test_transaction_object_commands(
transaction = ClusterTransaction() if cluster_mode else Transaction()
transaction.set(string_key, "foo")
transaction.object_encoding(string_key)
transaction.object_refcount(string_key)
# OBJECT FREQ requires a LFU maxmemory-policy
transaction.config_set({maxmemory_policy_key: "allkeys-lfu"})
transaction.object_freq(string_key)
Expand All @@ -530,14 +531,17 @@ async def test_transaction_object_commands(
assert response[0] == OK # transaction.set(string_key, "foo")
assert response[1] == "embstr" # transaction.object_encoding(string_key)
assert (
response[2] == OK
cast(int, response[2]) >= 0
) # transaction.object_refcount(string_key)
assert (
response[3] == OK
) # transaction.config_set({maxmemory_policy_key: "allkeys-lfu"})
assert cast(int, response[3]) >= 0 # transaction.object_freq(string_key)
assert cast(int, response[4]) >= 0 # transaction.object_freq(string_key)
assert (
response[4] == OK
response[5] == OK
) # transaction.config_set({maxmemory_policy_key: "allkeys-random"})
assert (
cast(int, response[5]) >= 0
cast(int, response[6]) >= 0
) # transaction.object_idletime(string_key)
finally:
await redis_client.config_set({maxmemory_policy_key: maxmemory_policy})

0 comments on commit f282542

Please sign in to comment.