Cachetools module in Python
Last Updated :
10 Jul, 2020
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
- LFUCache
- RRCache
Let's look at each of the following functions in detail and with examples.
Cached
cached is used as a decorator. When we call cache, it will cache the function for later use. This will by default perform a simple cache.
Syntax:
@cached(cache = {})
def some_fun():
pass
Example: Let's see it using an example. We are going to use the time module to see the efficiency of our module.
Python3
from cachetools import cached
import time
# Without cached
def fib(n):
return n if n<2 else fib(n-1) + fib(n-2)
s = time.time()
print(old_fib(35))
print("Time Taken: ", time.time() - s)
# Now using cached
s = time.time()
# Use this decorator to enable caching
@cached(cache ={})
def fib(n):
return n if n<2 else fib(n-1) + fib(n-2)
print(fib(35))
print("Time Taken(cached): ", time.time() - s)
Output:
9227465
Time Taken: 4.553245782852173
9227465
Time Taken(cached): 0.0003821849822998047
LRUCache
LRUCache is used inside the cached decorator. LRU cache means "Least Recently Used" cache. It takes a parameter "maxsize" which states that how recent functions should be cached.
Syntax:
@cached(cache= LRUCache(maxsize= 3))
def some_fun():
pass
Example:
Python3
from cachetools import cached, LRUCache
import time
# cache using LRUCache
@cached(cache = LRUCache(maxsize = 3))
def myfun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return (f"I am executed: {n}")
# Takes 3 seconds
print(myfun(3))
# Takes no time
print(myfun(3))
# Takes 2 seconds
print(myfun(2))
# Takes 1 second
print(myfun(1))
# Takes 4 seconds
print(myfun(4))
# Takes no time
print(myfun(1))
# Takes 3 seconds because maxsize = 3
# and the 3 recent used functions had 1,
# 2 and 4.
print(myfun(3))
Output:
Time Taken: 3.0030977725982666
I am executed: 3
I am executed: 3
Time Taken: 2.002072334289551
I am executed: 2
Time Taken: 1.001115083694458
I am executed: 1
Time Taken: 4.001702070236206
I am executed: 4
I am executed: 1
Time Taken: 3.0030171871185303
I am executed: 3
Note: LRUCache can also be called from the standard Python package - functools. It can see imported as
from functools import lru_cache
@lru_cache
def myfunc():
pass
TTLCache
TTLCache or "Time To Live" cache is the third function that is included in cachetools module. It takes two parameters - "maxsize" and "TTL". The use of "maxsize" is the same as LRUCache but here the value of "TTL" states for how long the cache should be stored. The value is in seconds.
Syntax:
@cached(cache= TTLCache(maxsize= 33, ttl = 600))
def some_fun():
pass
Example:
Python3
from cachetools import cached, TTLCache
import time
# Here recent 32 functions
# will we stored for 1 minutes
@cached(cache = TTLCache(maxsize = 32, ttl = 60))
def myfun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return (f"I am executed: {n}")
print(myfun(3))
print(myfun(3))
time.sleep(61)
print(myfun(3))
Output:
Time Taken: 3.0031025409698486
I am executed: 3
I am executed: 3
Time Taken: 3.0029332637786865
I am executed: 3
LFUCache
LFUCache or "Least Frequently Used" cache is another type of caching technique that retrieves how often an item is called. It discards the items which are called least often to make space when necessary. It takes one parameter - "maxsize" which is the same as in LRUCache.
Syntax:
@cached(cache= LFUCache(maxsize= 33))
def some_fun():
pass
Example:
Python3
from cachetools import cached, LFUCache
import time
# Here if a particular item is not called
# within 5 successive call of the function,
# it will be discarded
@cached(cache = LFUCache(maxsize = 5))
def myfun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return (f"I am executed: {n}")
print(myfun(3))
print(myfun(3))
print(myfun(2))
print(myfun(4))
print(myfun(1))
print(myfun(1))
print(myfun(3))
print(myfun(3))
print(myfun(4))
Output:
Time Taken: 3.002413272857666
I am executed: 3
I am executed: 3
Time Taken: 2.002107620239258
I am executed: 2
Time Taken: 4.003819465637207
I am executed: 4
Time Taken: 1.0010886192321777
I am executed: 1
I am executed: 1
I am executed: 3
I am executed: 3
I am executed: 4
RRCache
RRCache or "Random Replacement" cache is another type of caching technique that randomly chooses items in the cache and discards them to free up space when necessary. It takes one parameter - "maxsize" which is the same as in LRUCache. It also has a parameter choice which is by default set to "random.choice".
Syntax:
@cached(cache= RRCache(maxsize= 33))
def some_fun():
pass
Example:
Python3
from cachetools import cached, RRCache
import time
# Here if a particular item is not called
# within 5 successive call of the function,
# it will be discarded
@cached(cache = RRCache(maxsize = 5))
def myfun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return (f"I am executed: {n}")
print(myfun(3))
print(myfun(3))
print(myfun(2))
print(myfun(4))
print(myfun(1))
print(myfun(1))
print(myfun(3))
print(myfun(2))
print(myfun(3))
Output:
Time Taken: 3.003124713897705
I am executed: 3
I am executed: 3
Time Taken: 2.0021231174468994
I am executed: 2
Time Taken: 4.004120588302612
I am executed: 4
Time Taken: 1.0011250972747803
I am executed: 1
I am executed: 1
I am executed: 3
I am executed: 2
I am executed: 3
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read