#313: Persisted Cache for Function Calls
It is already 2 years since I wrote about built-in functools to cache calls to your Python functions. While this is still a great approach, it has one downside: when we restart our tool, the cache is empty and we need to do the computational work once more.
When it comes to persisting our cache, we have multiple options. A simple little tool that pops up in a lot of post I read in the last weeks is DiskCache. The last update of this library is from August 2023, what leads to questions if this library is still maintained. We should keep an eye on that but still give it a try. Let us see how we can use this tool for a persisted cache.
Installation
Before we can use DiskCache, we need to install it with this command:
A non-cached function
We can reuse the function example from the initial post to calculate the factorial of a number, where the print() statement is a stand-in for a more complex and time-consuming logic:
>>> factorial(5)
***5***
***4***
***3***
***2***
***1***
***0***
120
>>> factorial(6)
***6***
***5***
***4***
***3***
***2***
***1***
***0***
720
In our non-cached example, our function had to call print() 13 times.
The cached function
We can use DiskCache to create a persisted cache based on SQLite that goes into the .cache directory:
We put this code into the file diskcache_demo.py and see the reduced calls to the print() statement:
The big benefit shows when we run the script once more. Now we can get all the values from the cache and have none of the stand-in print() calls from the factorial() function:
Cache management
If we do not specify anything, the cached values will stay in our cache. That can be a problem if we have a real-world usage with many different inputs. We can influence how many seconds our values stay in the cache with the expire= parameter:
If you run the code before, you best delete the .cache folder to make sure that the expire time has an effect.
Conclusion
With DiskCache we get a small helper that allows us to persist our cache. That way we can reuse the cache between the runs of our script. But be aware that the values stay around and we need to think about cache management – or else our cache will grow. I like this approach a lot and hope that we soon get some positive news on the future development of DiskCache.