A decorator to keep results around for later
>>> from dontforget import cached >>> from time import sleep >>> >>> @cached ... def long_running(): ... print('Doing an expensive API call...') ... sleep(5) ... return 42 ... >>> long_running() Doing an expensive API call... 42 >>> long_running() 42
dontforget provides a caching decorator that you can apply to your
long-running functions to keep their outputs around for later. Results
are saved to local disk, so they'll still be around when you come back.
dontforget is like
functools.lru_cache() except that you get to
keep the results around between runs. It comes in handy when you have
expensive computation or data loading to perform as part of your pipeline,
but the results are unlikely to change between one run and the next.
dontforget is smart enough to bust your cache when:
- Your arguments change - hopefully that goes without saying; or
- Your function body changes - so you aren't re-using results from a computation you no longer care about
dontforget also accepts a cache-busting key to enable you to flush
state between releases, or whenever you feel like it.
- You want to keep results around between runs - so an
lru_cachewon't cut it
- You are willing to trade disk space for speed
- You have pure functions whose results don't change between invocations.
Don't consider using
- You call out to a live data source and don't want stale results
- Your disk won't live long enough to use the data you've hived off