From c2b0d3cd8178cbf92b3c4b38473371ac68195056 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Wed, 26 May 2021 11:35:53 +0100 Subject: [PATCH] Add the TYPE option for SCAN, available for redis 6.0 (only applies to SCAN and not SSCAN/HSCAN/ZSCAN) --- aredis/commands/iter.py | 11 ++++++++--- aredis/commands/keys.py | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/aredis/commands/iter.py b/aredis/commands/iter.py index 5fabd7fb..57019bdb 100644 --- a/aredis/commands/iter.py +++ b/aredis/commands/iter.py @@ -10,7 +10,7 @@ class IterCommandMixin: """ RESPONSE_CALLBACKS = {} - async def scan_iter(self, match=None, count=None): + async def scan_iter(self, match=None, count=None, type=None): """ Make an iterator using the SCAN command so that the client doesn't need to remember the cursor position. @@ -18,10 +18,13 @@ async def scan_iter(self, match=None, count=None): ``match`` allows for filtering the keys by pattern ``count`` allows for hint the minimum number of returns + + ``type`` filters results by a redis type """ cursor = '0' while cursor != 0: - cursor, data = await self.scan(cursor=cursor, match=match, count=count) + cursor, data = await self.scan(cursor=cursor, match=match, + count=count, type=type) for item in data: yield item @@ -80,7 +83,7 @@ async def zscan_iter(self, name, match=None, count=None, class ClusterIterCommandMixin(IterCommandMixin): - async def scan_iter(self, match=None, count=None): + async def scan_iter(self, match=None, count=None, type=type): nodes = await self.cluster_nodes() for node in nodes: if 'master' in node['flags']: @@ -91,6 +94,8 @@ async def scan_iter(self, match=None, count=None): pieces.extend(['MATCH', match]) if count is not None: pieces.extend(['COUNT', count]) + if type is not None: + pieces.extend(['TYPE', type]) response = await self.execute_command_on_nodes([node], 'SCAN', *pieces) cursor, data = list(response.values())[0] for item in data: diff --git a/aredis/commands/keys.py b/aredis/commands/keys.py index cdc67baa..ed868c72 100644 --- a/aredis/commands/keys.py +++ b/aredis/commands/keys.py @@ -249,7 +249,7 @@ async def wait(self, num_replicas, timeout): """ return await self.execute_command('WAIT', num_replicas, timeout) - async def scan(self, cursor=0, match=None, count=None): + async def scan(self, cursor=0, match=None, count=None, type=None): """ Incrementally return lists of key names. Also return a cursor indicating the scan position. @@ -257,12 +257,16 @@ async def scan(self, cursor=0, match=None, count=None): ``match`` allows for filtering the keys by pattern ``count`` allows for hint the minimum number of returns + + ``type`` filters results by a redis type """ pieces = [cursor] if match is not None: pieces.extend([b('MATCH'), match]) if count is not None: pieces.extend([b('COUNT'), count]) + if type is not None: + pieces.extend([b('TYPE'), type]) return await self.execute_command('SCAN', *pieces)