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.5.tar.gz (22.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.5-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: result_caching-0.5.tar.gz
  • Upload date:
  • Size: 22.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.5.tar.gz
Algorithm Hash digest
SHA256 62b43856e5738272e5a06d558541e689178ea7d3ce9d2bd598bec0e9a97258d6
MD5 337063def0ef803ac49e12716e3c2afd
BLAKE2b-256 0eddb491ce8a631e24fd4a142f4899214464bd7a0425765dc87b36fed376cfa5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: result_caching-0.5-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bdf0c9abc81c393871d7dbfa8cfb4fac66e36321482e8e59e6a62d13aa983eb0
MD5 e98fbe0da471f668ea782fdf3275c4a0
BLAKE2b-256 ed8547575004bb9222781100904d0d623aeff1a8f4cd442234c54e13b121a220

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5 This release

2 files

0.4

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