Skip to main content

Python library to cache data and function results in temporary files

Project description

Python library to cache data and function results in temporary files

This library offers a simple way to cache data and function results using temporary files. By default it will use the pickle module to hash key values and serialize output data. This is meant to be used with long running functions that have repeatable results. The hashing of keys is dependent on the pickle algorithm and so it may work accross different python environments, as long as they have a compatible pickling algorithm.

To avoid possible collisions make sure to use a unique name when instantiating TempCache. You can also add a source argument as any opaque string to further differentiate cache keys from other caches.

Note For more advanced use cases you may want to look at the Memory class in joblib.

Basic Usage

An instance of the TempCache class be used as a decorator to automatically cache the results of a function.

from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

@temp_cache
def long_running(...):
    ...

result = long_running(...)

Caching results at the call site

You can also use a TempCache object to cache a result at the call site with the cache_result method.

from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

def long_running(...):
    ...

result = temp_cache.cache_result(long_running, ...)

Advanced usage

In cases where the function or some of its arguments are defined in the __main__ namespace or in a jupyter notebook and cannot be pickled by pickle you may want to use a different pickle module like cloupickle.

import cloudpickle

from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache("tempcache-foo",
                       pickler=cloudpickle,
                       max_age=CACHE_MAX_AGE)

Examples

Examples notebooks are in the extras folder.

Installation

You can install this package with pip.

pip install tempcache

Related Projects

  • joblib Computing with Python functions
  • percache Persistently cache results of callables
  • cloudpickle Extended pickling support for Python objects

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

tempcache-0.0.7-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file tempcache-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: tempcache-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for tempcache-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 98e4c6f8878dc43f0f0d4c2937025dcb9120b0c1609f81552d51ec2764d5545e
MD5 2f18323444646927d3bb2551c0a3b664
BLAKE2b-256 fa1d3c9d7f8b57fde59ec5c69d3e626cbe2fd0daa07c2581314d2bcdd0fe8f04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page