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
Memoryclass 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tempcache-0.0.13-py3-none-any.whl.
File metadata
- Download URL: tempcache-0.0.13-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
509366067ecb8b0e74ed30730e747aab3cd4690265bfe8d761e4f859c7b9dbe9
|
|
| MD5 |
8c262eee48fd62144f83a76bbdfa62ed
|
|
| BLAKE2b-256 |
eb03d493ef4f03b9a359cf0ee00b16a082490a805c648f264a9d00b18cb5e4ea
|