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

Allow user to bypass the cache when calling the @cached-decorated coroutine #538

Closed
wants to merge 2 commits into from

Conversation

theomart
Copy link

@theomart theomart commented Apr 22, 2021

What do these changes do?

It allows the user of the cached decorator to choose a keyword argument he can pass to the decorated function to easily bypass the cache.

Are there changes in behavior for the user?

If bypass_kwarg is not specified, nothing changes for the user. The user can choose a different keyword for each decorated function to conveniently avoid conflicts with actual arguments passed to the non-cache related logic.

import aiocache
import asyncio

@aiocache.cached(bypass_kwarg='refresh')
async def decorated_function(a):
    await asyncio.sleep(3)
    return a
    
loop = asyncio.get_event_loop()
print(loop.run_until_complete(decorated_function('test')))
print(loop.run_until_complete(decorated_function('test', refresh=False)))
print(loop.run_until_complete(decorated_function('test', refresh=True)))

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> (e.g. 588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the PR
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: Fix issue with non-ascii contents in doctest text files.

@theomart theomart changed the title Add bypass_kwarg feature Allow user to bypass the cache when calling the @cached-decorated coroutine Apr 22, 2021
In case the decorated function calls a decorated function and so on...
@Dreamsorcerer
Copy link
Member

Seems like a decent idea, let me know if you want to come back and finish this off.

I'm thinking it might be cleaner if we just add a method or something to the decorated object, so you'd just do: my_func.refresh(*args) or similar.

We could also just clear the cache beforehand, which looks like it can already be done with something like my_func.cache.delete(key). With calculating the key being the difficult part, so a helper method to do that would still be good, so you just do my_func.clear(*args).

@Dreamsorcerer
Copy link
Member

I've opened #625 for this idea. Let me know if you want to work on it.

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

Successfully merging this pull request may close these issues.

2 participants