Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable decorators cache per-call #404

Merged
merged 3 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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