Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elecnix
Copy link

@elecnix elecnix commented Apr 26, 2025

What do these changes do?

Refactored @cached and @multi_cached to return a wrapper object providing cache control:

  • 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.

Are there changes in behavior for the user?

Users can now call refresh() and invalidate() 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

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

- 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.
@Dreamsorcerer
Copy link
Member

Maybe this one is fine to go in now. I'll review later.

Copy link
Member

@Dreamsorcerer Dreamsorcerer May 3, 2025

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.

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)
Copy link
Member

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?

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.

Wrap functions in class with decorator
2 participants