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
Functools module in Python
Functools module is for higher-order functions that work on other functions. It provides functions for working with other functions and callable objects to use or extend them without completely rewriting them. This module has two classes - partial and partialmethod. Partial class A partial function
6 min read
External Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Python Falcon - Inspect Module
In the world of web development, Python Falcon is a popular framework known for building high-performance APIs. One of the key components that makes Falcon powerful is its inspect module. This module provides utilities for introspection, which means examining the internal properties of Python object
3 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
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
__future__ Module in Python
__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. I
4 min read
Create and Import modules in Python
In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read
How to Install a Python Module?
A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read