Persistent Caching of Expensive Function Results
- Install with
pip install scrat - Initialize stash
scrat init - Start saving time:
import scrat
import time
@scrat.stash()
def expensive_function(param_1):
time.sleep(3)
return param_1
expensive_function(1) # <- function called
expensive_function(1) # <- function not called the result is recovered from stash
expensive_function(2) # <- function called again beacuse the parameters changed- Seamlessly stores the results of expensive functions to disk for future reuse.
- Automatically re-evaluates the function if the parameters or function code have changed, ensuring up-to-date results.
- Saves any result using the pickle.
- Improved storage of pandas DataFrames, Series, and Numpy arrays.
- Customizable support for alternative serializers.
- Flexible parameter hashing mechanism to efficiently handle any parameter type.
- Command-line interface (CLI) for convenient control and management of the caching functionality.
Great and fast memoize provided by the standard library functools, unfurtunately results are stored in memory so they can't be reused in different runs.
Provides alternatives to lru_cache but it also works in-memory.
Joblib is a stablished library that provides great functionality for parallelization and caching. The Memory module provides an excelent alternative to Scrat, but it does have some limitations:
- Hard to avoid using pickle
- Lack of options to control the cache size and policies
- Lack of tools to inspect and cleanup the cache
These are the problems that scrat aims to improve, however, I'd recommend using Joblib in production since it's much more mature than Scrat at the moment.
Scratis a famous pre-historic squirrel with some bad luckStashis composed of a folder where results are saved and a database to index them- A
Nutis one of the entries in the database - The
Squirrelis in charge of fetching and stashing theNuts Serializerdumps results to files and load them back to memoryHashercreates unique hashes for a parameter valueHashManagercoordinates hashes of all arguments and functon code
- Clone this repo
- Install pyenv.
- Install the python version used for development running
pyenv installin the root of this repository. - Install poetry. Version
1.5.1is recommended. - Run this command to make sure poetry uses the right python version
poetry env use $(which python) - Install project and dependencies with
poetry install - Run tests with
poetry run pytestor activate the virtualenv withpoetry shelland then runpytest
