Supercharge Your Functions with lru_cache
Learn how Python's lru_cache decorator from functools speeds up repeated function calls by caching results, with practical examples and tips on cache size management.
Here is the article you requested, written in the style of a human tech writer for PythonSkillset.com.
Supercharge Your Functions with lru_cache
Have you ever written a function that does the same expensive calculation over and over? You pass in the same arguments, and it churns away like it’s never seen them before. It can make your code feel sluggish.
There’s a neat trick in Python’s standard library that fixes this in about three lines of code. It’s called lru_cache from the functools module. Let me show you how it works at PythonSkillset.
What is lru_cache?
The letters stand for Least Recently Used cache. It’s a decorator that remembers the results of your function calls. When you call a function with the same arguments again, instead of running the whole function, it just returns the saved result.
Think of it like a sticky note on your desk. The first time you calculate something, you write the answer down. Next time you need it, you just glance at the note.
A Simple Example
Let’s take a classic slow function: calculating Fibonacci numbers. The naive recursive version is famous for being slow.
import functools
def fib_naive(n):
if n < 2:
return n
return fib_naive(n-1) + fib_naive(n-2)
If you call fib_naive(35), it takes a noticeable amount of time. It recalculates the same values thousands of times.
Now, let’s add the cache.
import functools
@functools.lru_cache(maxsize=None)
def fib_fast(n):
if n < 2:
return n
return fib_fast(n-1) + fib_fast(n-2)
Just adding that one line — @functools.lru_cache(maxsize=None) — changes everything. Calling fib_fast(35) now runs in a fraction of a millisecond. It remembers the result for every n it has seen.
Understanding the Cache Size
You might be wondering about maxsize=None. That tells the cache to grow without limits. It will remember every single unique call forever. This is fine for small input ranges.
But if your function takes millions of unique inputs, the cache will consume memory. That’s the trade-off.
You can set a specific size, like maxsize=128. Once the cache is full, it starts kicking out the results that were used the least recently. That’s the "LRU" part. It keeps the most useful answers.
When to Use It
You don’t need it for every function. Here’s when it shines at PythonSkillset:
- Expensive calculations that are repeated with the same inputs.
- Database queries that look up the same record often.
- API calls where the data doesn’t change frequently.
A Real-World Feel
Imagine you are building a web app and fetching user data from a database. The first time you ask for user 42, it queries the database. You call lru_cache on that function. The second time you need user 42, it returns instantly from the cache.
A junior developer might write a manual dictionary to store results. That works, but lru_cache adds automatic memory management and thread safety in a single line. It’s a small upgrade that feels like a superpower.
A Simple Rule
If your function is pure — meaning the same inputs always give the same output — and it is called repeatedly, slap @functools.lru_cache on it. Your program will thank you.
Next time you notice a function taking a bit too long, don’t overthink it. Just try the cache. It’s a tool that every Python developer should have in their pocket.
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.