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, including a mechanism for automatic expiration after a certain time.

Dy default, the package uses the pickle module to serialize data. Inputs are first serialized and then hashed to create unique cache file names.

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 wrap a function and cache its results.

from tempcache import TempCache

CACHE_MAX_AGE = 86_400  # one day
cache = TempCache("mycache", 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 function call directly at the call site with the cache_result method.

from tempcache import TempCache

CACHE_MAX_AGE = 86_400  # one day
cache = TempCache("mycache", max_age=CACHE_MAX_AGE)

def long_running(...):
    ...

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

Custom serialization

In cases where the inputs or result cannot be serialized by pickle you should use a pickler module like cloupickle.

import cloudpickle

from tempcache import TempCache

CACHE_MAX_AGE = 86_400  # one day
cache = TempCache("mycache",
                    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
  • disckcache Disk and file backed cache library compatible with Django
  • cloudpickle Extended pickling support for Python objects
  • cached_path A file utility for accessing both local and remote files through a unified interface

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.13-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for tempcache-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 509366067ecb8b0e74ed30730e747aab3cd4690265bfe8d761e4f859c7b9dbe9
MD5 8c262eee48fd62144f83a76bbdfa62ed
BLAKE2b-256 eb03d493ef4f03b9a359cf0ee00b16a082490a805c648f264a9d00b18cb5e4ea

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