Caching Method Responses with cachetools in Python

Improve your application’s performance by up to 500x 🚀

Raheel Siddiqui
2 min readAug 12, 2023

Caching is a technique for storing data temporarily so that it can be quickly accessed later. This can be useful for improving the performance of applications that make repeated requests to the same data source.

The cachetools library provides a number of tools for caching data in Python. In this article, we will see how to use the cached decorator to cache the response from a method.

The cached Decorator

The cached decorator takes a cache object as its argument. The cache object is responsible for storing and retrieving the cached data.

In the following example, we create a TTLCache object with a maximum size of 1024 items and a time-to-live (TTL) of 86400 seconds (24 hours).

from cachetools import cached
from cachetools import TTLCache

@cached(cache=TTLCache(maxsize=1024, ttl=86400))
def get_data():
"""Get some data from a slow API."""
time.sleep(1)
return {"data": "some data"}

The cached decorator will cache the result of the get_data() function for 24 hours. If the function is called again within 24 hours, the cached result will be returned without making another request to the API.

Example Usage

The following code demonstrates how to use the cached decorator to cache the response from a method:

import time

@cached(cache=TTLCache(maxsize=1024, ttl=86400))
def get_data():
"""Get some data from a slow API."""
time.sleep(1)
return {"data": "some data"}

def main():
start = time.time()
data = get_data()
end = time.time()

print("Time to get data:", end - start)
print("Data:", data)

if __name__ == "__main__":
main()

The first time the get_data() function is called, it will take 1 second to get the data from the API. The second time the function is called, the cached data will be returned immediately, and the time to get the data will be much faster.

In this example, the cached data will be stored for 24 hours. After 24 hours, the cached data will expire, and the get_data() function will be called again to get the latest data from the API.

Conclusion

The cachetools library is a powerful tool for caching data in Python. The cached decorator makes it easy to cache the results of methods, which can significantly improve the performance of applications that make repeated requests to the same data source.

I hope this article has been helpful!

Feel free to share your thoughts about python backends. You can reach out to me at Linkedin, twitter.

connect me at raheelsiddiqui.com

--

--

Raheel Siddiqui

Full Stack Engineer | AWS UG Leader | GitHub Campus Expert | Former Google DSC Lead | AWS Community Builder | Opensource FTW✨