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 1 commit
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
39 changes: 26 additions & 13 deletions aiocache/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ async def wrapper(*args, **kwargs):
return wrapper

async def decorator(self, f, *args, **kwargs):
disable_read = kwargs.pop('aiocache_disable_read', False)
disable_write = kwargs.pop('aiocache_disable_write', False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put explicitly in the function signature please

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, add docs about those two new parameters in the class docstring or in decorators.rst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

key = self.get_cache_key(f, args, kwargs)

value = await self.get_from_cache(key)
if value is not None:
return value
if not disable_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 not disable_write:
await self.set_in_cache(key, result)

return result

Expand Down Expand Up @@ -241,18 +246,24 @@ async def wrapper(*args, **kwargs):
return wrapper

async def decorator(self, f, *args, **kwargs):
disable_read = kwargs.pop('aiocache_disable_read', False)
disable_write = kwargs.pop('aiocache_disable_write', False)

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 not disable_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 @@ -261,7 +272,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 not disable_write:
await self.set_in_cache(result, args, kwargs)

return result

Expand Down
63 changes: 63 additions & 0 deletions tests/ut/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ 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_disable_read(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(return_value=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this if its not being called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably from copy&pasting, removed


await decorator_call(aiocache_disable_read=True)

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_disable_write(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(return_value=None)

await decorator_call(aiocache_disable_write=True)

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(
aiocache_disable_read=True,
aiocache_disable_write=True,
)

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 +465,38 @@ 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_disable_read(self, decorator, decorator_call):
decorator.cache.multi_get = CoroutineMock(return_value=[1, 2])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, not needed because its not being called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


await decorator_call(1, keys=['a', 'b'], aiocache_disable_read=True)

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_disable_write(self, decorator, decorator_call):
decorator.cache.multi_get = CoroutineMock(return_value=[None, None])

await decorator_call(1, keys=['a', 'b'], aiocache_disable_write=True)

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'],
aiocache_disable_read=True,
aiocache_disable_write=True,
)

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