Skip to content

Commit

Permalink
sscan now takes cursor as an argument, and returns a tuple of `curs…
Browse files Browse the repository at this point in the history
…or, result`
  • Loading branch information
amirreza8002 committed Dec 23, 2024
1 parent 5cca181 commit 93e97d2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
11 changes: 8 additions & 3 deletions django_valkey/async_cache/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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

Expand Down
9 changes: 6 additions & 3 deletions django_valkey/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions tests/tests_async/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 93e97d2

Please sign in to comment.