Skip to content

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

@jeremyschiemann

Description

@jeremyschiemann

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions