-
Notifications
You must be signed in to change notification settings - Fork 172
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
Open
elecnix
wants to merge
1
commit into
aio-libs:master
Choose a base branch
from
elecnix:class-wrapper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,12 +44,38 @@ def __init__( | |
self.cache = cache | ||
|
||
def __call__(self, f): | ||
@functools.wraps(f) | ||
async def wrapper(*args, **kwargs): | ||
return await self.decorator(f, *args, **kwargs) | ||
class CachedFunctionWrapper: | ||
def __init__(self, decorator, func): | ||
functools.update_wrapper(self, func) | ||
self._decorator = decorator | ||
self._func = func | ||
self.cache = decorator.cache | ||
|
||
async def __call__(self, *args, **kwargs): | ||
return await self._decorator.decorator(self._func, *args, **kwargs) | ||
|
||
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) | ||
Comment on lines
+57
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
wrapper.cache = self.cache | ||
return wrapper | ||
return CachedFunctionWrapper(self, f) | ||
|
||
async def decorator( | ||
self, f, *args, cache_read=True, cache_write=True, aiocache_wait_for_write=True, **kwargs | ||
|
@@ -229,12 +255,38 @@ def __init__( | |
self.ttl = ttl | ||
|
||
def __call__(self, f): | ||
@functools.wraps(f) | ||
async def wrapper(*args, **kwargs): | ||
return await self.decorator(f, *args, **kwargs) | ||
class CachedFunctionWrapper: | ||
def __init__(self, decorator, func): | ||
functools.update_wrapper(self, func) | ||
self._decorator = decorator | ||
self._func = func | ||
self.cache = decorator.cache | ||
|
||
async def __call__(self, *args, **kwargs): | ||
return await self._decorator.decorator(self._func, *args, **kwargs) | ||
|
||
async def refresh(self, *args, **kwargs): | ||
""" | ||
Force a cache refresh for the given key. | ||
|
||
This method recomputes the result by calling the original function, | ||
then updates the cache with the new value for the given key. | ||
""" | ||
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 provided key, or clear all if no key is provided.""" | ||
if not args and not kwargs: | ||
await self.cache.clear() | ||
else: | ||
# Invalidate each key in args | ||
for key in args: | ||
cache_key = self._decorator.key_builder(key, self._func, *args, **kwargs) | ||
await self.cache.delete(cache_key) | ||
|
||
wrapper.cache = self.cache | ||
return wrapper | ||
return CachedFunctionWrapper(self, f) | ||
|
||
async def decorator( | ||
self, f, *args, cache_read=True, cache_write=True, aiocache_wait_for_write=True, **kwargs | ||
|
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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:
All other logic can exist in the Wrapper class.