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.

The package uses the pickle module by default to serialize data and hash key values.

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
cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

@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
cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

def long_running(...):
    ...

result = 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 can use a different pickle module like cloupickle.

import cloudpickle

from tempcache import TempCache

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

key = ...
# key object can be complex as long as it it pickeable

item = cache.item_for_key(key)
# cache item for the given key wether it exists or not

value = item.load()

if value is None:
    value = compute(...)
    item.save(value)

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
  • disckcache Disk and file backed cache library compatible with Django
  • 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

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

tempcache-0.0.9-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tempcache-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for tempcache-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 f27a93041a3234df24d0f8ab6169ff58793adb4a8995851b81f150d63d04e7b5
MD5 81ed98885232eadf7e4c7bf6d1bc1c89
BLAKE2b-256 0706ea7ae5ac16e8d26527e6d2d97d2af7c18928af427b097d64d0d903f3fe10

See more details on using hashes here.

Supported by

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