Clear LRU Cache in Python
Last Updated :
10 Jul, 2020
The
LRU is the Least Recently Used cache. LRU Cache is a type of high-speed memory, that is used to quicken the retrieval speed of frequently used data. It is implemented with the help of Queue and Hash data structures.
Note: For more information, refer to
Python – LRU Cache
How can one interact with the LRU Cache in Python?
Python's functool module has provided functionality to interact with the LRU Cache since Python 3.2. The functool module offers a decorator that can be placed atop a Class of a function in Python. When used on functions that require large amounts of variable access and change operations, using the LRU Cache offers massive speed-up.
Example:
Python3 1==
import functools
@functools.lru_cache(maxsize = None)
def gfg():
# insert function logic here
pass
Alternatively, the maxsize can be changed to suit one's own preference. The value is measured in kbs, and maxsize takes an integer argument
Clearing LRU Cache
After the use of the cache, cache_clear() can be used for clearing or invalidating the cache.
Example 1:
Python3 1==
import functools
@functools.lru_cache(maxsize = None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
fib(30)
# Before Clearing
print(fib.cache_info())
fib.cache_clear()
# After Clearing
print(fib.cache_info())
Output:
CacheInfo(hits=28, misses=31, maxsize=None, currsize=31)
CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)
Example 2: Additionally one can also call cache_clear() from another function as well
Python3 1==
import functools
@functools.lru_cache(maxsize = None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
def gfg():
fib.cache_clear()
fib(30)
# Before Clearing
print(fib.cache_info())
gfg()
# After Clearing
print(fib.cache_info())
Output:
CacheInfo(hits=28, misses=31, maxsize=None, currsize=31)
CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)
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. We can overcome this problem, by using Python's inbuilt garbage collection module to collect all objects that have LRU Cache Wrappers, and iteratively clear each object's cache.
Example:
Python3 1==
import gc
import functools
@functools.lru_cache(maxsize = None)
def gfg():
# insert function logic here
pass
@functools.lru_cache(maxsize = None)
def gfg1():
# insert function logic here
pass
@functools.lru_cache(maxsize = None)
def gfg2():
# insert function logic here
pass
gfg()
gfg1()
gfg2()
gc.collect()
# All objects collected
objects = [i for i in gc.get_objects()
if isinstance(i, functools._lru_cache_wrapper)]
# All objects cleared
for object in objects:
object.cache_clear()
Similar Reads
Python - LRU Cache LRU Cache is the least recently used cache which is basically used for Memory Organization. In this, the elements come as First in First Out format. We are given total possible page numbers that can be referred to. We are also given cache (or memory) size (Number of page frames that cache can hold a
4 min read
Python Functools - lru_cache() The functools module in Python deals with higher-order functions, that is, functions operating on(taking as arguments) or returning functions and other such callable objects. The functools module provides a wide array of methods such as cached_property(func), cmp_to_key(func), lru_cache(func), wraps
2 min read
Implementing LRU Cache Decorator in Python LRU is the cache replacement algorithm that removes the least recently used data and stores the new data. Suppose we have a cache space of 10 memory frames. And each frame is filled with a file. Now if we want to store the new file, we need to remove the oldest file in the cache and add the new file
3 min read
Cachetools module in Python Cachetools is a Python module which provides various memoizing collections and decorators. It also includes variants from the functools' @lru_cache decorator. To use it, first, we need to install it using pip. pip install cachetools Cachetools provides us five main function. cached LRUCache TTLCache
5 min read
Python Dictionary clear() clear() method in Python is used to remove all items (key-value pairs) from a dictionary. After calling this method, the dictionary will become empty and its length will be 0. This method is part of the built-in dictionary operations in Python.Example:Pythond = {1: "geeks", 2: "for"} # using clear()
2 min read
Matplotlib.axes.Axes.clear() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
1 min read