Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use cache_clear() on python @functools.lru_cache

The documentation states:

The decorator also provides a cache_clear() function for clearing or invalidating the cache.

It doesn't provide any examples or guidance on how to use cache_clear()

I have two questions:

  • How can I run cache_clear() from a different function?
  • If I put a cache_clear() call conditionally inside the function that is being cached, will it ever get executed?
like image 261
cjm2671 Avatar asked Jun 06 '16 09:06

cjm2671


People also ask

How do you use Functools in Python?

It can be used in key functions such as sorted(), min(), max(). It applies a function of two arguments repeatedly on the elements of a sequence so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x^y, [1, 2, 3, 4]) calculates (((1^2)^3)^4) .

How does Python Lru_cache work?

Python's @lru_cache decorator offers a maxsize attribute that defines the maximum number of entries before the cache starts evicting old items. By default, maxsize is set to 128 . If you set maxsize to None , then the cache will grow indefinitely, and no entries will be ever evicted.

What is Functools function in Python?

The functools module, part of Python's standard Library, provides useful features that make it easier to work with high order functions (a function that returns a function or takes another function as an argument ).

Which function is imported from Functools module?

from functools import lru_cache, singledispatch.


2 Answers

Besides caching, lru_cache decorator also adds new functions, to the decorated function - cache_info and cache_clear. Below is a simple example that should explain how they work:

>>> @lru_cache(5) ... def foo(): ...     print('Executing foo...') ...  >>> foo() Executing foo... >>> foo() >>> foo.cache_info() CacheInfo(hits=1, misses=1, maxsize=5, currsize=1) >>> foo.cache_clear() >>> foo() Executing foo... 

Answering your questions:

If I put a cache_clear() call conditionally inside the function that is being cached, will it ever get executed?

If the result is not cached already, the function will execute and based on your conditions, it should execute cache_clear. I wouldn't use such solution though - a good practise is to invalidate outside the cached object, otherwise you risk no invalidation at all in worst cases, unreadable code in best case.

How can I run cache_clear() from a different function?

Just import cached function and call cache_clear on it:

from x import foo  def bar():     foo.cache_clear() 
like image 121
matino Avatar answered Sep 18 '22 06:09

matino


If the method you are trying to expire the cache for is a property:

class Foo:     @property     @lru_cache()     def bar(self):        return 42 

Then you can clear the cache this way:

Foo.bar.fget.cache_clear() 

See this answer: https://stackoverflow.com/a/55497384/8953378

like image 32
Alon Gouldman Avatar answered Sep 20 '22 06:09

Alon Gouldman