Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear cache of @property methods python

I have some property methods in a Class and I want to clear the cache of this property at some point.

Example :

class Test():
    def __init__(self):
        pass

    @property
    @functools.lru_cache()
    def prop(self):
        # Compute some stuffs and return complex number

if I do self.prop.clear_cache(), here the error message I get :

AttributeError: 'numpy.complex128' object has no attribute 'cache_clear'

clear_cache() works for functions but not for property methods. Is there any way to do that?

like image 409
pensfute Avatar asked Apr 03 '19 14:04

pensfute


People also ask

How do I clear data cache in Python?

After the use of the cache, cache_clear() can be used for clearing or invalidating the cache. These methods have limitations as they are individualized, and the cache_clear() function must be typed out for each and every LRU Cache utilizing the function.

How do you cache a function in Python?

To memoize a function in Python, we can use a utility supplied in Python's standard library—the functools. lru_cache decorator. Now, every time you run the decorated function, lru_cache will check for a cached result for the inputs provided. If the result is in the cache, lru_cache will return it.

How do I use LRU cache in Python?

One way to implement an LRU cache in Python is to use a combination of a doubly linked list and a hash map. The head element of the doubly linked list would point to the most recently used entry, and the tail would point to the least recently used entry.

Are properties cached Python?

cached_property is a decorator that converts a class method into a property whose value is calculated once and then cached like a regular attribute. The cached value will be available until the object or the instance of the class is destroyed.


1 Answers

You need to access the cache attributes on the getter attribute of the property object, so .fget. You can only access the property object on the class:

Test.prop.fget.cache_clear()

That's because the @property decorator replaced the prop function object with LRU cache, with a property instance.

Accessing the property name on an instance will always give you the result of the property getter, not the function object with cache controls.

Demo:

>>> import functools
>>> class Foo:
...     @property
...     @functools.lru_cache()
...     def bar(self):
...         print("Accessing the bar property")
...         return 42
...
>>> f = Foo()
>>> f.bar
Accessing the bar property
42
>>> f.bar  # cached
42
>>> Foo.bar.fget.cache_clear()
>>> f.bar
Accessing the bar property
42

Note that using a LRU cache this way means the cache storage is shared across all instances of the class, with separate results stored per instance (the cache is keyed on the self parameter). Clearing the cache will clear it for all instances. Given the default maxsize=128 configuration, that means that only the property value for the most recently-used 128 instances will be cached. Access the property on instance #129 and then again an instance #1 will mean that the value is re-calculated for #1 as that instance's property value will have been evicted.

like image 181
Martijn Pieters Avatar answered Sep 19 '22 00:09

Martijn Pieters