Wrap decorators and add cache control API (#625)#985
Open
elecnix wants to merge 1 commit intoaio-libs:masterfrom
Open
Wrap decorators and add cache control API (#625)#985elecnix wants to merge 1 commit intoaio-libs:masterfrom
elecnix wants to merge 1 commit intoaio-libs:masterfrom
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.
Member
|
Maybe this one is fine to go in now. I'll review later. |
Member
There was a problem hiding this comment.
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.
Comment on lines
+57
to
+76
| 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) |
Member
There was a problem hiding this comment.
Can we skip this in the initial PR and add it in a followup PR?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
Refactored
@cachedand@multi_cachedto 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