From 2408737556fb9f7cba87550e068ac75647d437a9 Mon Sep 17 00:00:00 2001 From: Pedro Artiga Date: Mon, 27 Sep 2021 16:22:52 -0700 Subject: [PATCH] - Add delete_many to Redis --- HISTORY.rst | 5 +++++ aioradio/redis.py | 27 +++++++++++++++++++++++++++ aioradio/tests/redis_test.py | 16 ++++++++++++++++ setup.py | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index 5db1a39..1d66dbf 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,11 @@ History ======= +v0.14.4 (2021-09-27) + +* Add function delete_many to Redis. + + v0.14.3 (2021-07-30) * Add SentTimestamp attribute to SQS get_messages function. diff --git a/aioradio/redis.py b/aioradio/redis.py index ca1572a..ca02381 100644 --- a/aioradio/redis.py +++ b/aioradio/redis.py @@ -147,6 +147,33 @@ async def delete(self, key: str) -> int: return self.pool.delete(key) + async def delete_many(self, pattern: str, max_batch_size: int=500) -> int: + """Delete all keys it matches the desired pattern + + Args: + pattern (str): cache key pattern + max_batch_size (int): size of pipeline batch, defaults to 500, can be adjusted to increase performance + + Returns: + int: total of deleted keys + """ + pipe = self.pool.pipeline() + total = 0 + batch_size = 0 + + for key in self.pool.scan_iter(pattern): + pipe.delete(key) + total = total + 1 + batch_size = batch_size + 1 + + if batch_size == max_batch_size: + pipe.execute() + batch_size = 0 + + pipe.execute() + + return total + async def hget(self, key: str, field: str, use_json: bool=None, encoding: Union[str, None]=None) -> Any: """Get the value of a hash field. diff --git a/aioradio/tests/redis_test.py b/aioradio/tests/redis_test.py index c42b51a..b7266fa 100644 --- a/aioradio/tests/redis_test.py +++ b/aioradio/tests/redis_test.py @@ -133,3 +133,19 @@ async def test_get_many_items(cache): await cache.set(key='pytest-3', value='three') results = await cache.mget(['pytest-1', 'pytest-2', 'pytest-3']) assert results == ['one', 'two', 'three'] + +async def test_delete_many_items(cache): + """Test delete_many""" + + await cache.set(key='delete-many-1', value='one') + await cache.set(key='delete-many-2', value='two') + await cache.set(key='delete-many-3', value='three') + + results = await cache.mget(['delete-many-1', 'delete-many-2', 'delete-many-3']) + assert results == ['one', 'two', 'three'] + + total = await cache.delete_many('delete-many*') + assert total == 3 + + results = await cache.mget(['delete-many-1', 'delete-many-2', 'delete-many-3']) + assert results == [None, None, None] diff --git a/setup.py b/setup.py index 56e522e..2bff270 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = fileobj.read() setup(name='aioradio', - version='0.14.3', + version='0.14.4', description='Generic asynchronous i/o python utilities for AWS services (SQS, S3, DynamoDB, Secrets Manager), Redis, MSSQL (pyodbc), JIRA and more', long_description=long_description, long_description_content_type="text/markdown",