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

add warning in multi_cached docs about reusing keys #388

Closed
jcugat opened this issue Mar 12, 2018 · 3 comments
Closed

add warning in multi_cached docs about reusing keys #388

jcugat opened this issue Mar 12, 2018 · 3 comments

Comments

@jcugat
Copy link
Contributor

jcugat commented Mar 12, 2018

With the @cached decorator, there's no problem in using it with different methods since it will create different prefixes based on the method signature.

@cached(alias='default')
async def foo1(self):
    return 1

@cached(alias='default')
async def foo2(self):
    return 2

assert await foo1() == 1
assert await foo2() == 2

I found a bit surprising that it's not the same for @multi_cached, this example will create clashing keys if the identifiers are the same:

@multi_cached('entity_ids', alias='default')
async def multi1(self, entity_ids):
    return {num: num for num in entity_ids}

@multi_cached('entity_ids', alias='default')
async def multi2(self, entity_ids):
    return {num: num*2 for num in entity_ids}

# This works
assert await multi1(entity_ids=[1, 2]) == {1: 1, 2: 2}
# This would fail, since it would return the cached results from the previous call
assert await multi2(entity_ids=[1, 2]) == {1: 2, 2: 4}
@argaen
Copy link
Member

argaen commented Mar 12, 2018

In the case of cached, you are caching the function result while for multi_cached, you store each key from your entity_ids individually. The idea of this was that later in other parts of your code you could load those values easily doing cache.get('entity_id').

This problem can be solved with https://github.com/argaen/aiocache/blob/master/aiocache/decorators.py#L200 and also if we implement #387 by setting a different namespace for example.

@jcugat
Copy link
Contributor Author

jcugat commented Mar 15, 2018

Oh, I understand the reasoning now. Maybe it's worth updating the documentation with this difference between the two.

@argaen
Copy link
Member

argaen commented Mar 20, 2018

Yup, worth to add a warning in the documentation of multi_cached. Will leave this issue as reminder to do so

@argaen argaen changed the title @multi_cached decorator should use different prefixes by default add warning in multi_cached docs about reusing keys Mar 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants