The Wayback Machine - https://web.archive.org/web/20240118170111/https://www.geeksforgeeks.org/clear-lru-cache-in-python/
Open In App
Related Articles

Clear LRU Cache in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:




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:




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




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:




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



Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 10 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials