-
Notifications
You must be signed in to change notification settings - Fork 166
Wrap decorators and add cache control API (#625) #985
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
base: master
Are you sure you want to change the base?
Conversation
- Refactored @cached and @multi_cached to return a wrapper object with .refresh() and .invalidate() methods. - refresh: forces a cache update for the given key. - invalidate: clears the cache for the given key, or clears all if no key is given. Closes aio-libs#625. Replaces aio-libs#927.
Maybe this one is fine to go in now. I'll review later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you refer back to the example in the issue I posted, my intention was to replace the cached class completely. This currently feels just as awkward as before with the wrapper class referencing the decorator class.
My expectation is that the cached decorator will literally just be defined as:
def cached(func):
return Wrapper(func)
All other logic can exist in the Wrapper class.
async def refresh(self, *args, **kwargs): | ||
""" | ||
Force a refresh of the cache. | ||
|
||
This method recomputes the result by calling the original function, | ||
then updates the cache with the new value for the given arguments. | ||
""" | ||
return await self._decorator.decorator( | ||
self._func, *args, cache_read=False, cache_write=True, **kwargs | ||
) | ||
|
||
async def invalidate(self, *args, **kwargs): | ||
""" | ||
Invalidate the cache for the given key, or clear all cache if no key is provided. | ||
""" | ||
if not args and not kwargs: | ||
await self.cache.clear() | ||
else: | ||
key = self._decorator.get_cache_key(self._func, args, kwargs) | ||
await self.cache.delete(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we skip this in the initial PR and add it in a followup PR?
What do these changes do?
Refactored
@cached
and@multi_cached
to return a wrapper object providing cache control:Are there changes in behavior for the user?
Users can now call
refresh()
andinvalidate()
on decorated functions or methods to control the cache directly.No breaking changes to existing cache usage patterns.
Related issue number
Fixes #625
Replaces #927
Checklist