Cache¶
- class surfalize.cache.CachedInstance¶
Bases:
objectMixin class that provides the basic facilities necessary for the cache decorator as well as a method to clear the cache.
Methods
Clears the cache for the entire instance.
create_cache_entry(method, entry, args, kwargs)Manually creates a cache entry for the specified method.
- clear_cache()¶
Clears the cache for the entire instance.
- Returns:
- None
- create_cache_entry(method, entry, args, kwargs)¶
Manually creates a cache entry for the specified method.
- Parameters:
- methodfunction pointer
method to cache.
- entryany
return value that will be cached
- args
arguments to the method call for which to create the cache entry
- kwargs
keyword arguments to the method call for which to create the cache entry
- Returns:
- None
- surfalize.cache.cache(method)¶
Decorator that enables caching on class instance without creating memory leaks. This is accomplished by relying on a cache inside the instance. Classes that want to use this decorator must implement a dictionary called ‘_method_cache’ or inherit from CachedInstance. This approach is necessary because functools.lru_cache keeps references to the class instance that prevent it from being garbage collected indefinitely, thus leaking a substantial amount of memory. See https://bugs.python.org/issue19859 for more details.
This implementation will not work on methods with unhashable arguments.
- Parameters:
- methodmethod
method to be decorated.
- Returns:
- wrapped_method