Skip to content

Commit

Permalink
- Add delete_many to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Artiga committed Sep 27, 2021
1 parent 8791da1 commit 2408737
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions aioradio/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions aioradio/tests/redis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2408737

Please sign in to comment.