28
Add Caching to Any Python Function in One Line
Did you know that Python actually has built-in decorators that can add a cache to any function? (what are decorators?)
Let's say I have a function that requires a bit a computing power. For this example, I'll use a fibonacci sequence calculator.
def fib(x):
if x <= 1:
return 1
return fib(x - 1) + fib(x)
Using Python's builtin
functools
library, I can add a "cache" in one line. A cache will remember the outputs of a compute-heavy function so the values don't need to be calculated multiple times.If a set of inputs have already been calculated, the
cache
decorator will return the calculated result without running the fib()
function.from functools import cache
@cache
def fib(x):
if x <= 1:
return 1
return fib(x - 1) + fib(x)
However,
@cache
will store an unlimited number of inputs and outputs. If we want to limit it to only, say, 1,000 cached values, we can use a "least-recently-used" or "LRU" cache.With this type of cache, Python will only cache the 1,000 most recently used values. If a new value is calculated, the 1,001st value will be "evicted" from the cache (removed from memory).
Example 1 (note that we do not need parentheses):
from functools import lru_cache
@lru_cache
def fib(x):
if x <= 1:
return 1
return fib(x - 1) + fib(x)
Example 2 (maximum size of 1000 elements):
from functools import lru_cache
@lru_cache(max_size=1000)
def fib(x):
if x <= 1:
return 1
return fib(x - 1) + fib(x)
28