Skip to main content

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 .py files invalidate the cache
  • LRU eviction: configurable via max_cache_bytes or CASHER_MAX_CACHE_BYTES env 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

Or set programmatically (takes priority over env vars):

from casher import configure, get_config

configure(cache_dir="/data/my_cache", max_cache_bytes=10 * 1024**3)
print(get_config())  # effective config

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:

License

MIT

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

pycasher-0.4.1.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycasher-0.4.1-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

Details for the file pycasher-0.4.1.tar.gz.

File metadata

  • Download URL: pycasher-0.4.1.tar.gz
  • Upload date:
  • Size: 70.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pycasher-0.4.1.tar.gz
Algorithm Hash digest
SHA256 4a2ea497d62cc95ce10655037a85c48b8256fae49cb74165fd57eccce34d31f5
MD5 65ffe56c9043540f23bf197618a27a3a
BLAKE2b-256 73a960a641d3b7f5e27a24f7f1f0fa3fd39a1a539b9d42c1054c974c6415d3f2

See more details on using hashes here.

File details

Details for the file pycasher-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: pycasher-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 38.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pycasher-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4ace41084837d905583d74ef3e25ac7bad04ed03ece6af1b9374637ac77a38f
MD5 3705d3b5e33cb335bebdd9032d53f29c
BLAKE2b-256 4a6f67a7c50534f1f9c36e28abc45d1bddb7dcd02cb10184b81b838c2741d1c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page