Cache function results to disk
Project description
Function Cacher
Overview
The Function Cacher project provides a way to cache the result of a (e.g. computation or IO-heavy) function to a location on the filesystem, ideally a tmpfs.
In comparison to Python’s integrated functools.cache, this approach provides caching outside of a programs lifetime. The (originally) intended use-case is caching results of long-running database queries during rapid-prototyping (& rapidly crashing) data analysis scripts.
Please refer to https://function-cacher.readthedocs.io for documentation, more examples, and further details.
Tested on Python 3.10 and upwards.
Installation
Use pip to install this package:
pip install functionCacher
The package is located here: https://pypi.org/project/functionCacher/
Basic Usage
from functionCacher.Cacher import Cacher
cacher_instance = Cacher()
@cacher_instance.cache
def myfunc(*args):
print(f"myfunc is getting called with {args}")
return args
print(myfunc(1,2,3)) # cache miss; prints "myfunc is getting..."
print(myfunc(1,2,3)) # cache hit ; prints nothing
print(myfunc(2,3,4)) # cache miss; prints "myfunc is getting..."
print(f"Cache path is: {cacher_instance.cachePath}")
Output:
% python3 basic_example.py
myfunc is getting called with (1, 2, 3)
(1, 2, 3)
(1, 2, 3)
myfunc is getting called with (2, 3, 4)
(2, 3, 4)
Cache path is: /path/to/cachedir
Features
- cache function results to disk
pickles objects for storage- cache compression (LZMA, ZSTD)
- HMAC-verification of cache file (do not forget to set a proper
hmac_keyduring Cacher initialisation!) - cache decorator
- automatic cache ID generation, based on
- function parameter
- function name
- function source code
Notes & Caveats
-
Caches are on the filesystem. For maximum performance, you might want to consider a RAM-backed FS like a ramdisk or tmpfs.
-
FunctionCacher assumes pure functions without side-effects. It can only discern two cache-entries via the function parameters!
-
Python
picklemight mess with your cached function results. Works well in most cases, though.
Related Projects
While this project has been developed largely in a vacuum (sorry! Didn't know better), several related projects approach result caching in Python in similar or identical ways:
cache_to_diskby sarenehan (https://github.com/sarenehan/cache_to_disk)- Shelved Cache by mhelf (https://pypi.org/project/shelved-cache)
asynccacheby hexhep (https://github.com/hephex/asyncache)
Licence
MIT Licence
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 functionCacher-1.0.5.2.tar.gz.
File metadata
- Download URL: functionCacher-1.0.5.2.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecfdbb55e37903a50a70820329339623efc26ae5c02ad6405bd469b59ba17ef2
|
|
| MD5 |
56b5d67a8591f36c8420595f7d768125
|
|
| BLAKE2b-256 |
9cd5f774bfe9ea3ff2efaeecf04e3976f1a1ae1e34b5c1291fe540f5af99b062
|
File details
Details for the file functionCacher-1.0.5.2-py3-none-any.whl.
File metadata
- Download URL: functionCacher-1.0.5.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b46c58a2ebe52031c0fab315eb6360c739426d3860679d1717494a9bbcd872ef
|
|
| MD5 |
abf6c8c7308cf7055e2fc6709eedfc9c
|
|
| BLAKE2b-256 |
9ac346322e58a65b4c7b884a833f1306a64fe9ffc79bb342ed64ca01345f39f4
|