From 93e97d2f41462913f5bdc7bab217bd71209a1bd9 Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 23 Dec 2024 11:54:06 +0330 Subject: [PATCH] sscan now takes `cursor` as an argument, and returns a tuple of `cursor, result` --- django_valkey/async_cache/client/default.py | 11 ++++++++--- django_valkey/base_client.py | 9 ++++++--- tests/test_backend.py | 6 ++++-- tests/tests_async/test_backend.py | 6 ++++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/django_valkey/async_cache/client/default.py b/django_valkey/async_cache/client/default.py index 3d29005..915daec 100644 --- a/django_valkey/async_cache/client/default.py +++ b/django_valkey/async_cache/client/default.py @@ -990,11 +990,13 @@ async def srem( async def sscan( self, key, + cursor: int = 0, match: str | None = None, count: int = 10, version: int | None = None, client: AValkey | Any | None = None, - ) -> Set[Any]: + return_set: bool = True, + ) -> tuple[int, Set[Any]] | tuple[int, list[Any]]: # TODO check this is correct if self._has_compression_enabled() and match: error_message = "Using match with compression is not supported." @@ -1004,11 +1006,14 @@ async def sscan( key = await self.make_key(key, version=version) cursor, result = await client.sscan( - key, + name=key, + cursor=cursor, match=cast(PatternT, await self.encode(match)) if match else None, count=count, ) - return {await self.decode(value) for value in result} + return cursor, await self._decode_iterable_result( + result, convert_to_set=return_set + ) asscan = sscan diff --git a/django_valkey/base_client.py b/django_valkey/base_client.py index b688192..8ef22b1 100644 --- a/django_valkey/base_client.py +++ b/django_valkey/base_client.py @@ -1093,11 +1093,13 @@ def srem( def sscan( self, key: KeyT, + cursor: int = 0, match: str | None = None, count: int = 10, version: int | None = None, client: Backend | Any | None = None, - ) -> Set[Any]: + return_set: bool = True, + ) -> tuple[int, Set[Any]] | tuple[int, list[Any]]: if self._has_compression_enabled() and match: err_msg = "Using match with compression is not supported." raise ValueError(err_msg) @@ -1107,11 +1109,12 @@ def sscan( key = self.make_key(key, version=version) cursor, result = client.sscan( - key, + name=key, + cursor=cursor, match=cast(PatternT, self.encode(match)) if match else None, count=count, ) - return {self.decode(value) for value in result} + return cursor, self._decode_iterable_result(result, convert_to_set=return_set) def sscan_iter( self, diff --git a/tests/test_backend.py b/tests/test_backend.py index 5ea91f5..bb8890e 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -1248,15 +1248,17 @@ def test_srem(self, cache: ValkeyCache): def test_sscan(self, cache: ValkeyCache): cache.sadd("foo", "bar1", "bar2") - items = cache.sscan("foo") + cursor, items = cache.sscan("foo") assert items == {"bar1", "bar2"} + assert cursor == 0 def test_sscan_with_match(self, cache: ValkeyCache): if cache.client._has_compression_enabled(): pytest.skip("Compression is enabled, sscan with match is not supported") cache.sadd("foo", "bar1", "bar2", "zoo") - items = cache.sscan("foo", match="zoo") + cursor, items = cache.sscan("foo", match="zoo") assert items == {"zoo"} + assert cursor == 0 def test_sscan_iter(self, cache: ValkeyCache): cache.sadd("foo", "bar1", "bar2") diff --git a/tests/tests_async/test_backend.py b/tests/tests_async/test_backend.py index 5485814..9ab7bed 100644 --- a/tests/tests_async/test_backend.py +++ b/tests/tests_async/test_backend.py @@ -1154,15 +1154,17 @@ async def test_srem(self, cache: AsyncValkeyCache): async def test_sscan(self, cache: AsyncValkeyCache): await cache.asadd("foo", "bar1", "bar2") - items = await cache.asscan("foo") + cursor, items = await cache.asscan("foo") assert items == {"bar1", "bar2"} + assert cursor == 0 async def test_sscan_with_match(self, cache: AsyncValkeyCache): if cache.client._has_compression_enabled(): pytest.skip("Compression is enabled, sscan with match is not supported") await cache.asadd("foo", "bar1", "bar2", "zoo") - items = await cache.asscan("foo", match="zoo") + cursor, items = await cache.asscan("foo", match="zoo") assert items == {"zoo"} + assert cursor == 0 async def test_sscan_iter(self, cache: AsyncValkeyCache): await cache.asadd("foo", "bar1", "bar2")