Skip to main content

Build Status

Result Caching

Stores function results so that they are not computed again on repetitive calls of the function with the same arguments. Results can be stored either on disk or in memory.

Quick setup

pip install git+https://github.com/mschrimpf/result_caching

Usage example

from result_caching import store

@store()
def f(a, b):
	return a * b
	
y = f(1, 2)  # computed first time, stored on disk
y = f(1, 2)  # not computed again, loaded from disk
y = f(1, 3)  # computed again, different parameters

By default, results will be stored in ~/.result_caching, this can be changed through the environment variable RESULTCACHING_HOME.

cache will only hold results in memory and not write them to disk.

Environment variables

Variable description
RESULTCACHING_HOME directory to cache results (benchmark ceilings) in, ~/.result_caching by default
RESULTCACHING_DISABLE * '1' to disable loading and saving of results, functions will be called directly
* 'candidate_models.score_model,model_tools.activations' to disable loading and saving of function identifiers starting with one of the specifiers separated by a comma (e.g. any package or function inside model_tools.activations will not be considered)
RESULTCACHING_CACHEDONLY If enabled, raises an error when trying to run a function that does not have its result already cached (follows the same matching rules as RESULTCACHING_DISABLE)
RESULTCACHING_FORMAT storage format for xarray results: pickle (default) or netcdf/nc. netCDF writes a .nc file plus a sidecar .manifest.json recording the class, dtype, shape, schema version and package versions
RESULTCACHING_S3_BUCKET S3 bucket for xarray results. Setting this is what enables the S3 backend — unset means disk only. Requires the s3 extra (pip install result_caching[s3])
RESULTCACHING_S3_PREFIX key prefix within the bucket, result_caching by default
RESULTCACHING_S3_EPOCH prefix segment for bulk invalidation, 1 by default. Bumping it orphans every existing entry at once, which is cheaper than deleting them when a lifecycle rule will expire them anyway
RESULTCACHING_S3_MAX_GB refuse to write entries larger than this, 50 by default. The size distribution has a long tail — one vision model projects to 146 GB against a p90 of 31 GB — and that tail is most of the storage bill for a fraction of the benefit

Storage formats

pickle (the default) is fast but tied to the exact pandas/xarray/numpy versions that wrote it. netcdf is portable across environment upgrades, at the cost of flattening MultiIndex coordinates on write and rebuilding them on load.

The default is deliberately still pickle so that existing warm caches stay valid; opt in per environment rather than globally.

Either way an unreadable entry is treated as a miss, never an error — a cache written before a dependency bump is recomputed rather than raising.

S3 backend

Set RESULTCACHING_S3_BUCKET to make xarray results readable and writable across machines, which is what makes the cache useful for ephemeral containers where local disk does not survive the job.

Entries are always netCDF on S3 regardless of RESULTCACHING_FORMAT; the data object is written before its manifest and a read requires the manifest, so an interrupted write is indistinguishable from a miss and needs no temp-key dance (S3 PUTs are already atomic).

boto3 is an optional dependency. If it is missing, the backend degrades to "no cache" rather than breaking the import.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

result_caching-0.4.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

result_caching-0.4-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file result_caching-0.4.tar.gz.

File metadata

  • Download URL: result_caching-0.4.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.14

File hashes

Hashes for result_caching-0.4.tar.gz
Algorithm Hash digest
SHA256 50a9ffa92e7e4489f8ad4bc26ba96cd072ab3a2afd135595cd17dd50eff4333b
MD5 7e32470c9d4c707258752e49e86c2f43
BLAKE2b-256 79785f3d90e89870da275fdd8d788e6d1e6e1348c6b1c43d4a694ef6688efd41

See more details on using hashes here.

File details

Details for the file result_caching-0.4-py3-none-any.whl.

File metadata

  • Download URL: result_caching-0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.14

File hashes

Hashes for result_caching-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e04a1a9d77731ff268381734fa9c96b2a3ea77dbd830df252b8c1cc80952c82d
MD5 46aa628796b818b68680d2496fe69f9b
BLAKE2b-256 2810f6cf0d32d909e620048e1f4ef4ea03fc0980ed8780f3233e9f56ff8ba623

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5

2 files

This release

0.4 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page