From d595959e6136920258e3d9caec64b964acdb8207 Mon Sep 17 00:00:00 2001 From: Jeppe Klitgaard Date: Tue, 29 Sep 2020 00:38:40 +0100 Subject: [PATCH] fix: cache control arguments for cached_stampede `cached_stampede` now features the same cache control arguments as the `cached` decorator --- aiocache/decorators.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/aiocache/decorators.py b/aiocache/decorators.py index 9b87e6c7..710c7e9f 100644 --- a/aiocache/decorators.py +++ b/aiocache/decorators.py @@ -183,12 +183,15 @@ def __init__(self, lease=2, **kwargs): super().__init__(**kwargs) self.lease = lease - async def decorator(self, f, *args, **kwargs): + async def decorator( + self, f, *args, cache_read=True, cache_write=True, aiocache_wait_for_write=True, **kwargs + ): key = self.get_cache_key(f, args, kwargs) - value = await self.get_from_cache(key) - if value is not None: - return value + if cache_read: + value = await self.get_from_cache(key) + if value is not None: + return value async with RedLock(self.cache, key, self.lease): value = await self.get_from_cache(key) @@ -197,7 +200,11 @@ async def decorator(self, f, *args, **kwargs): result = await f(*args, **kwargs) - await self.set_in_cache(key, result) + if cache_write: + if aiocache_wait_for_write: + await self.set_in_cache(key, result) + else: + asyncio.ensure_future(self.set_in_cache(key, result)) return result