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 basic method to
cache data and functions to temporary files.
By default it will use the pickle
module
to hash key values and to serialize data in temporary files.
It is meant to work with long running functions
whose results can be re-used. To avoid possible collisions
make sure to use a unique name when instantiating TempCache
.
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 the latest version of this module with pip
.
python -mpip install git+https://github.com/furechan/tempcache.git
Related Projects
- joblib Computing with Python functions
- 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
Built Distribution
File details
Details for the file tempcache-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: tempcache-0.0.5-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73195d5694dedf555de4de5c2850813e2ad68b57b6aa8d72791473e0c64def60 |
|
MD5 | feef70c0e9e536f588af1689fea5a88e |
|
BLAKE2b-256 | 5d9cfeb843de2f2bfb397aa69977189c6ed095bd0cdbe0d4490d85e4b99d873b |