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 2648a46 commit 6fcbecf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 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 @@ -970,11 +970,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 @@ -984,11 +986,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 @@ -1082,11 +1082,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 @@ -1096,11 +1098,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
12 changes: 10 additions & 2 deletions django_valkey/client/sharded.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,24 @@ def srem(
def sscan(
self,
key: KeyT,
cursor: int = 0,
match: str | None = None,
count: int = 10,
version: int | None = None,
client: Valkey | Any | None = None,
) -> Set[Any]:
return_set: bool = True,
) -> tuple[int, Set[Any]] | tuple[int, list[Any]]:
if client is None:
key = self.make_key(key, version=version)
client = self.get_server(key)
return super().sscan(
key=key, match=match, count=count, version=version, client=client
key=key,
cursor=cursor,
match=match,
count=count,
version=version,
client=client,
return_set=return_set,
)

def sscan_iter(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,15 +1247,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 @@ -1150,15 +1150,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 6fcbecf

Please sign in to comment.