Skip to content

Commit

Permalink
Can disable cache in decorated functions (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcugat authored and argaen committed May 27, 2018
1 parent 48cab91 commit 6d95ea9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
44 changes: 29 additions & 15 deletions aiocache/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class cached:
Only one cache instance is created per decorated call. If you expect high concurrency of calls
to the same function, you should adapt the pool size as needed.
When calling the decorated function, the reads and writes from/to the cache can be controlled
with the parameters ``cache_read`` and ``cache_write`` (both are enabled by default).
:param ttl: int seconds to store the function call. Default is None which means no expiration.
:param key: str value to set as key for the function return. Takes precedence over
key_builder param. If key and key_builder are not passed, it will use module_name
Expand Down Expand Up @@ -72,15 +75,18 @@ async def wrapper(*args, **kwargs):
wrapper.cache = self.cache
return wrapper

async def decorator(self, f, *args, **kwargs):
async def decorator(self, f, *args, cache_read=True, cache_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

result = await f(*args, **kwargs)
await self.set_in_cache(key, result)

if cache_write:
await self.set_in_cache(key, result)

return result

Expand Down Expand Up @@ -196,6 +202,9 @@ class multi_cached:
Only one cache instance is created per decorated function. If you expect high concurrency
of calls to the same function, you should adapt the pool size as needed.
When calling the decorated function, the reads and writes from/to the cache can be controlled
with the parameters ``cache_read`` and ``cache_write`` (both are enabled by default).
:param keys_from_attr: arg or kwarg name from the function containing an iterable to use
as keys to index in the cache.
:param key_builder: Callable that allows to change the format of the keys before storing.
Expand Down Expand Up @@ -241,19 +250,22 @@ async def wrapper(*args, **kwargs):
wrapper.cache = self.cache
return wrapper

async def decorator(self, f, *args, **kwargs):
async def decorator(self, f, *args, cache_read=True, cache_write=True, **kwargs):
missing_keys = []
partial = {}
keys, new_args, args_index = self.get_cache_keys(f, args, kwargs)

values = await self.get_from_cache(*keys)
for key, value in zip(keys, values):
if value is None:
missing_keys.append(key)
else:
partial[key] = value
if values and None not in values:
return partial
if cache_read:
values = await self.get_from_cache(*keys)
for key, value in zip(keys, values):
if value is None:
missing_keys.append(key)
else:
partial[key] = value
if values and None not in values:
return partial
else:
missing_keys = list(keys)

if args_index > -1:
new_args[args_index] = missing_keys
Expand All @@ -262,7 +274,9 @@ async def decorator(self, f, *args, **kwargs):

result = await f(*new_args, **kwargs)
result.update(partial)
await self.set_in_cache(result, args, kwargs)

if cache_write:
await self.set_in_cache(result, args, kwargs)

return result

Expand Down
52 changes: 52 additions & 0 deletions tests/ut/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ async def test_calls_get_and_returns(self, decorator, decorator_call):
assert decorator.cache.set.call_count == 0
assert stub.call_count == 0

@pytest.mark.asyncio
async def test_cache_read_disabled(self, decorator, decorator_call):
await decorator_call(cache_read=False)

assert decorator.cache.get.call_count == 0
assert decorator.cache.set.call_count == 1
assert stub.call_count == 1

@pytest.mark.asyncio
async def test_cache_write_disabled(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(return_value=None)

await decorator_call(cache_write=False)

assert decorator.cache.get.call_count == 1
assert decorator.cache.set.call_count == 0
assert stub.call_count == 1

@pytest.mark.asyncio
async def test_disable_params_not_propagated(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(return_value=None)

await decorator_call(cache_read=False, cache_write=False)

stub.assert_called_once_with()

@pytest.mark.asyncio
async def test_get_from_cache_returns(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(return_value=1)
Expand Down Expand Up @@ -434,6 +460,32 @@ async def test_calls_fn_raises_exception(self, mocker, decorator, decorator_call
with pytest.raises(Exception):
assert await decorator_call(keys=[])

@pytest.mark.asyncio
async def test_cache_read_disabled(self, decorator, decorator_call):
await decorator_call(1, keys=['a', 'b'], cache_read=False)

assert decorator.cache.multi_get.call_count == 0
assert decorator.cache.multi_set.call_count == 1
assert stub_dict.call_count == 1

@pytest.mark.asyncio
async def test_cache_write_disabled(self, decorator, decorator_call):
decorator.cache.multi_get = CoroutineMock(return_value=[None, None])

await decorator_call(1, keys=['a', 'b'], cache_write=False)

assert decorator.cache.multi_get.call_count == 1
assert decorator.cache.multi_set.call_count == 0
assert stub_dict.call_count == 1

@pytest.mark.asyncio
async def test_disable_params_not_propagated(self, decorator, decorator_call):
decorator.cache.multi_get = CoroutineMock(return_value=[None, None])

await decorator_call(1, keys=['a', 'b'], cache_read=False, cache_write=False)

stub_dict.assert_called_once_with(1, keys=['a', 'b'])

@pytest.mark.asyncio
async def test_set_in_cache(self, decorator, decorator_call):
await decorator.set_in_cache({'a': 1, 'b': 2}, (), {})
Expand Down

0 comments on commit 6d95ea9

Please sign in to comment.