Questions And Answers

More Tutorials

Python Function Caching

Caching means storing the data in a place from where it can be served faster. In the case of data that has been frequently used, the computer assigns a cache memory, so it does not have to load it again and again from the main memory. The purpose of the cache is to make the tasks more efficient and quicker. The same is true for web browsers; the pages we load again and again are stored in the cache for faster retrieval. In Python, however, we have to do it all manually, as the program will not store anything in the cache itself.

How to use function caching in Python?

Function caching is a way to improve code's performance by storing the function's return values. Before the 3.2 updates of Python, we had to create a cache ourselves by storing the value in a variable or by other such means. But in Python 3.2, there is a new update in the functools module of Python. To use this module, we have to import it first.

import functools

We have been facilitated with the help of a decorator known as lru_cache. Decorators are an essential part of Python. Decorators in Python can be used for a variety of different purposes.

We have to pass maxsize as a parameter with the decorator. maxsize is defined to tell the program how many values we want to store in the cache. It automatically stores the values for the latest number of calls.

For Example

def myfunc(x):
    return x

# myfunc(1) takes 2 seconds and results for myfunc(1) are now cached

We set the maxsize equal to 4, and the program uses the same call five times. Then, the program will only be able to retrieve the data faster for the last five calls because caching is only storing data for them. It is important to define the maxsize as per our requirements because it takes up memory accordingly, so for a better program, it should be precisely according to our needs.


import time
from functools import lru_cache

def some_work(n):
    #Some task taking n seconds
    return n

if __name__ == '__main__':
    print("Now running some work")
    print("Done... Calling again")
    print("Called again")


In this page (written and validated by ) you learned about Python Function Caching . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Else and Finally In Try Except.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.