Persisting a Cache in Python to Disk using a decorator
Caches are important in helping to solve time complexity issues, and ensure that we don’t run a time-consuming program twice. You never know when your scripts can just stop abruptly, and then you lose all the information in your cache, and you have you run everything all over again.
In order to counter this, saving your cache to a disk is something that can be very helpful in that it allows state to be saved to disk, and be retrieved from it anytime as long as its there.
The function below can be used as a decorator for methods to ensure that the cache is persisted to the disk with a specific filename you can determine to wrap around the original function.
You can then use the decorator as such:
A gist for the code is located here.