I have been searching a bit for a python module that offers a memoize decorator with the following capabilities:
I realize this is a 2-year-old question, and that this wouldn't count as an "established" decorator, but…
This is simple enough that you really don't need to worry about only using established code. The module's docs link to the source because, in addition to being useful in its own right, it works as sample code.
So, what do you need to add? Add a filename
parameter. At run time, pickle.load
the filename into the cache
, using {}
if it fails. Add a cache_save
function that just pickle.save
s the cache to the file under the lock. Attach that function to wrapper
the same as the existing ones (cache_info
, etc.).
If you want to save the cache automatically, instead of leaving it up to the caller, that's easy; it's just a matter of when to do so. Any option you come up with—atexit.register
, adding a save_every
argument so it saves every save_every
misses, …—is trivial to implement. In this answer I showed how little work it takes. Or you can get a complete working version (to customize, or to use as-is) on GitHub.
There are other ways you could extend it—put some save-related statistics (last save time, hits and misses since last save, …) in the cache_info
, copy the cache and save it in a background thread instead of saving it inline, etc. But I can't think of anything that would be worth doing that wouldn't be easy.