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 and hash key values.
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 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 is pickle-able
item = cache.item_for_key(key)
# cache item for the given key whether it exists or not
# load item if it exists
if item.exists():
value = item.load()
# save item
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
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.10-py3-none-any.whl.
File metadata
- Download URL: tempcache-0.0.10-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c3a5dbad1165dab818e6861bb78aee3ff961733fc95c8c816be75206c05c4db
|
|
| MD5 |
dcfe0b7ed4f6a70748e97e1c6fd24bab
|
|
| BLAKE2b-256 |
c560a06e577dd389ebc4aa55918269c3e35f16c9feef134211ac860b8b63354e
|