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. This library is best suited for slow or expensive functions that return large or complex results. Each item is saved as a separate file whose name is computed by serializing the inputs and hashing the result into a unique file name.
Basic usage
Caching is done through a TempCache class instance that manages cache items in a dedicated caching folder.
The first parameter should be the name to use as temp sub-folder or alternatively the absolute path of the cache folder.
The caching folder will be created if it does not already exist.
A TempCache instance can be used as a decorator to wrap a function and automatically cache its results.
Optional parameters include max_age (expiry in seconds) and pickler (custom pickler like cloudpickle for non-picklable objects).
from tempcache import TempCache
cache = TempCache("mycache", max_age=86_400) # One day
@cache.wrap
def long_running(...):
...
result = long_running(...)
Caching results at the call site
You can also use a TempCache instance to cache a function call directly
at the call site with the cache_result method.
from tempcache import TempCache
cache = TempCache("mycache", max_age=86_400) # One day
def long_running(...):
...
result = cache.cache_result(long_running, ...)
Manual caching
For fine-grained control you can manage cache entries directly. Any pickle-able value can be used as a key — it gets hashed into a digest string that identifies the cache entry.
cache = TempCache("mycache", max_age=86_400)
digest = cache.key_digest(my_key)
result = cache.try_load(digest)
if result is None:
result = compute(my_key)
cache.try_save(digest, result)
Clearing the cache
Expired items are removed automatically on read, but you can also trigger cleanup explicitly.
cache.clear_items() # remove expired items
cache.clear_items(all_items=True) # remove everything
Isolating caches with source
If two caches share the same folder, identical keys will collide. Use source to namespace them:
cache_a = TempCache("mycache", source="feed-a")
cache_b = TempCache("mycache", source="feed-b")
The same key will produce different digests in each cache.
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
- cachier Persistent function caching with TTL, supports pickle and MongoDB backends
- cachetools In-memory caching with LRU, TTL and other eviction strategies
- klepto Scientific caching library supporting file, memory and database backends
Metadata
Release files for tempcache 0.0.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tempcache-0.0.14-py3-none-any.whl | Python 3 | none | any | Details for tempcache-0.0.14-py3-none-any.whl |
Release files / tempcache-0.0.14-py3-none-any.whl
| Download URL | tempcache-0.0.14-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9f1888f422fab2a3697c0aaf4cfeabb930944b9a9e636c9b7551ddfb4deed1ed
|
|
BLAKE2b-256 checksum How to use checksums |
1b81700f665b6d741075929cc2b92b20ec30e09f58af0063bd163532be73fd21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Security for tempcache 0.0.14
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.