Skip to content

Commit

Permalink
added scan method
Browse files Browse the repository at this point in the history
  • Loading branch information
amirreza8002 committed Dec 21, 2024
1 parent f03532b commit f77437d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django_valkey/async_cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AsyncValkeyCache(BaseValkeyCache[AsyncDefaultClient, AValkey]):

iter_keys = BaseValkeyCache.aiter_keys

scan = BaseValkeyCache.ascan

ttl = BaseValkeyCache.attl

pttl = BaseValkeyCache.apttl
Expand Down
29 changes: 29 additions & 0 deletions django_valkey/async_cache/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,35 @@ async def keys(

akeys = keys

async def scan(
self,
cursor: int = 0,
match: PatternT | None = None,
count: int | None = None,
_type: str | None = None,
version: int | None = None,
client: AValkey | None = None,
) -> tuple[int, list[str]]:
if self._has_compression_enabled() and match:
raise ValueError("Using match with compression enables is not supported")
client = await self._get_client(write=False, client=client)

try:
cursor, result = await client.scan(
cursor=cursor,
match=(
await self.make_pattern(match, version=version) if match else None
),
count=count,
_type=_type,
)
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

return cursor, [self.reverse_key(val.decode()) for val in result]

ascan = scan

async def make_key(
self, key, version: int | None = None, prefix: str | None = None
):
Expand Down
8 changes: 8 additions & 0 deletions django_valkey/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ def keys(self, *args, **kwargs) -> list[Any]:
async def akeys(self, *args, **kwargs) -> list[Any]:
return await self.client.akeys(*args, **kwargs)

@omit_exception
def scan(self, *args, **kwargs) -> tuple[int, list[str]]:
return self.client.scan(*args, **kwargs)

@omit_exception
async def ascan(self, *args, **kwargs) -> tuple[int, list[str]]:
return await self.client.ascan(*args, **kwargs)

@omit_exception
def sadd(self, *args, **kwargs) -> int:
return self.client.sadd(*args, **kwargs)
Expand Down
25 changes: 25 additions & 0 deletions django_valkey/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,31 @@ def keys(
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

def scan(
self,
cursor: int = 0,
match: PatternT | None = None,
count: int | None = None,
_type: str | None = None,
version: int | None = None,
client: Backend | None = None,
) -> tuple[int, list[str]]:
if self._has_compression_enabled() and match:
raise ValueError("Using match with compression enables is not supported")
client = self._get_client(write=False, client=client)

try:
cursor, result = client.scan(
cursor=cursor,
match=self.make_pattern(match, version=version) if match else None,
count=count,
_type=_type,
)
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client) from e

return cursor, [self.reverse_key(val.decode()) for val in result]

def make_key(
self, key: KeyT, version: int | None = None, prefix: str | None = None
) -> KeyT:
Expand Down

0 comments on commit f77437d

Please sign in to comment.