Cache function results and side effects (stdout, stderr, file writes) with automatic file I/O discovery via strace or audit hooks
Project description
pycasher
Cache Python function results and their side effects — stdout, stderr, and filesystem writes — with automatic invalidation.
pip install pycasher
What makes it different
Most caching libraries cache return values. casher also captures and replays:
- stdout/stderr printed during execution
- Files written by the function (restored from cache on hit)
- Files read by the function (used as cache keys — change an input file, cache auto-invalidates)
No manual file declarations needed. casher discovers file I/O automatically via strace (subprocess mode) or Python audit hooks (in-process mode).
Usage
from casher import cached
@cached
def train(data_path: str, output_path: str, lr: float = 0.01) -> dict:
df = read_csv(data_path)
model = fit(df, lr=lr)
save(model, output_path)
return {"accuracy": model.score}
# First call — runs function, traces file I/O, caches everything
result = train("train.csv", "model.pkl")
# Second call — instant replay from cache (model.pkl restored too)
result = train("train.csv", "model.pkl")
# Change train.csv — casher detects it, re-runs automatically
Cache any shell command without code changes:
acache -- python train.py --data train.csv
Key features
- Automatic file tracking: strace (kernel-level, catches C extensions) or audit hooks (zero overhead, Python-only)
- Dependency invalidation: changes to imported
.pyfiles invalidate the cache - LRU eviction: configurable via
max_cache_bytesorCASHER_MAX_CACHE_BYTESenv var (default 32 GB) - DataFrame support: polars and pandas DataFrames serialized via Arrow IPC
- Environment-aware: include env vars in cache key with
env_vars=["MY_VAR"] - Structured logging: loguru INFO for every call — cache dir, hit/miss, mode, eviction
Configuration
| Env var | Default | Description |
|---|---|---|
CASHER_CACHE_DIR |
~/.cache/casher |
Cache storage directory |
CASHER_MAX_CACHE_BYTES |
34359738368 (32 GB) |
Max cache size before LRU eviction |
Platform support
Full caching on Linux only (requires strace for subprocess mode, fcntl for locking). On macOS and Windows the decorator is a transparent pass-through — functions execute normally, caching is skipped with a one-time warning.
Documentation
See documentation/ for detailed docs:
- Introduction — architecture, limitations, in-process vs subprocess comparison
- Quick Start — installation, decorator options, CLI usage
- API Reference — full API surface
License
MIT
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 pycasher-0.1.0.tar.gz.
File metadata
- Download URL: pycasher-0.1.0.tar.gz
- Upload date:
- Size: 67.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4eee17051f8b9dd4cf0af96a4dff2d849a0c95cedaaac34b1e3436350f31b3e
|
|
| MD5 |
038990c382dea7206a954d2f41d747ff
|
|
| BLAKE2b-256 |
f6fa78cec8bcc749ccba9fe45d3baefd988f8d9ade145e6d191b58aab71c55a7
|
File details
Details for the file pycasher-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pycasher-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d58701d05a169459bd98aa7956632cd2b45e5a8c9db494c8000d9feb9c0c87d0
|
|
| MD5 |
f2642d2482ca062b72ffc58e68ea01b2
|
|
| BLAKE2b-256 |
d6a2728e1bb2e47e9b0383c5bbd61fba3de75929a7e307c8894882efd0894e69
|