Simple disk-based function result caching using Python decorators
Project description
hashcache
Simple disk-based function result caching using Python decorators.
hashcache is a lightweight, caching decorator that stores function results to disk based on their arguments.
The goal
Lightweight, minimalist, easy to interpret (no complex eviction policies) and fast. Originally designed for caching stages of data processing pipelines, but can be used for any function 👀.
Installation
pip install hashcache
Quick start
from hashcache import hashcache
import time
@hashcache()
def f(value):
time.sleep(1)
return value
print(f(1)) # First call takes 1s
print(f(1)) # Second call returns instantly from cache
print(f(1, use_cache=False)) # Bypass cache, takes 1s again
Clearing the cache
The intention of this library is minimalism and simplicity, so there is no built-in cache management. The cache should be cleared periodically and after potentially breaking changes to the code
rm -r _hashcache_dir
Cache Control Arguments
The Decorator extracts the following args from the function call (they will not be passed onwards to the function):
| Argument | Type | Default | Description |
|---|---|---|---|
| use_cache | bool | True | Skip or use the cache |
| refresh_cache | bool | False | Force re-computation and overwrite cache |
| cache_nonce | Any | None | Used to get multiple results from a non-deterministic function with the same arguments. |
| use_dill_for_keys | bool | False | Use dill for serialization instead of pickle. |
Limitation (Important!)
By default, the cache key is generated using pickle, which does not include class method definitions. This will lead to stale cache results if the behavior of a class method changes.
from hashcache import hashcache
class MyClass:
def method(self):
return "original result"
@hashcache()
def function_with_cache(obj: MyClass):
return obj.method()
# Returns original result
print(function_with_cache(MyClass()))
# Redefine method after caching
def method(self):
return "updated result"
MyClass.method = method
# Still returns "original result" (cached)
print(function_with_cache(MyClass()))
# Returns "updated result", uses dill for accurate function serialization
# But much slower
print(function_with_cache(MyClass(), use_dill_for_keys=True))
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 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 hashcache-0.1.0.tar.gz.
File metadata
- Download URL: hashcache-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0f0e6abc75d6e7d7120f08f0fd4c4249401dc0609143ef706b44beb34da91f2
|
|
| MD5 |
6cb0a93754529168ac42253c3c4e7fcb
|
|
| BLAKE2b-256 |
184630f147ded42561db16a21d983d9f861a73c4804c879db558292693aa0eb1
|
File details
Details for the file hashcache-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hashcache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3b57800df1b16daa8ca32134ef95fc0925bf1965baf28961fea7a8703967911
|
|
| MD5 |
8000fe1164b045542b1f832768502da6
|
|
| BLAKE2b-256 |
ad598f21dc924d9e9aaebb1dea0daca5ad44366435f470c56e67509c296421e0
|