oitg.cache API

Minimalist caching/memoisation implementation backed by on-disk pickle files.

Provides functions for transparently storing expensive-to-compute values to disk and reading them back when they are next used.

oitg.cache.DIR_ENV_VAR = 'OITG_CACHE_DIR'

Name of the environment variable to load cache path from.

oitg.cache.get_cache_dir() Path[source]

Return the OITG-wide directory to use for cache files.

The default path can be overwritten by setting the DIR_ENV_VAR environment variable.

oitg.cache.read_or_create_pickle_cache(key: str, compute_value: Callable[[], Any]) Any[source]

Attempt to read the cached value for the given key; if it does not exist, compute it and save it for future calls.

Assumes compute_value() is pure (i.e. returns the same value every time); no no support for cache invalidation is provided beyond manually clearing a key using clear_pickle_cache().

Parameters:
  • key – Cache key, to be used as part of the file name. The user must ensure that names are unique across client code.

  • compute_value – Function to invoke for actually computing the value if it has not been cached yet.

Returns:

The computed or loaded value.

oitg.cache.clear_pickle_cache(key: str) None[source]

Remove the cache file for the given key, if it exists.

oitg.cache.cache_to_pickle_file(key)[source]

Wrap a parameterless function to cache its results to a pickle file and reload it from there on subsequent invocations.

See read_or_create_pickle_cache().