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

Is there an easy way to clear the cache? #22

Open
jeremyschiemann opened this issue Aug 27, 2024 · 2 comments
Open

Is there an easy way to clear the cache? #22

jeremyschiemann opened this issue Aug 27, 2024 · 2 comments

Comments

@jeremyschiemann
Copy link

jeremyschiemann commented Aug 27, 2024

Sometimes I need to invalidate the cache or need an empty cache in my tests. I didn't see any simple solution to clear it.


Currently im doing it like this:

from cache import AsyncTTL


@AsyncTTL(...)
async def my_function(...):
   ...

in order to clear the cache I have to this:

my_function.__closure__[1].cell_contents.ttl.clear()

it would be nice to have this function easily accessible like (same as functools.lru):

my_function.clear_cache()

and maybe even have access to the non-wrapped function with:

my_function.__wrapped__

which would be useful to disable cache during tests my mocking the cached function in conftest.py:

@pytest.fixture(auto_use=True)
def no_cache(monkeypatch):
   monkeypatch.setattribute(my_module, 'my_function', my_module.my_function.__wrapped__)

Would be a nice improvement if something like this would be implemented :)

@MarijnHoum
Copy link
Contributor

Hi!
I just ran into the same issue. The PR #23 has a solution.

@iamsinghrajat
Copy link
Owner

Thanks @MarijnHoum for adding this feature, will be a great help.
Will close this issue when new version is released.

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

No branches or pull requests

3 participants