Skip to main content

scratchcache

CI

scratchcache provides a small local read-through cache for files that are expensive to read directly, such as files stored on a network drive.

The package currently exposes one helper, local_machine_cache, which takes a source file path and a local cache directory. It returns a path to a local copy that can be passed to normal file-reading code.

Why It Exists

Some libraries repeatedly read the same input file and perform poorly when that file lives on slower remote storage. scratchcache lets callers keep their source-of-truth path unchanged while reading from a machine-local copy whenever the cached copy is current.

The cache is for reads only. Do not write through the returned path.

How It Works

local_machine_cache(fname, local_machine_cache_dir):

  • verifies that fname exists, otherwise raises FileNotFoundError
  • hashes the source path to choose a stable cache filename
  • preserves the source file's full extension chain, such as .tar.gz
  • creates the cache directory if needed
  • copies the source file with shutil.copy2 when the cache file is missing or its modification time differs from the source
  • returns the cached local pathlib.Path

Freshness is based on exact modification-time equality between the source file and the cached copy.

Installation

pip install scratchcache

The package supports Python 3.8 and newer and has no runtime dependencies.

Usage

from pathlib import Path

from scratchcache import local_machine_cache

source = Path("/Volumes/shared/data/example.csv")
cache_dir = Path("/tmp/scratchcache")

local_path = local_machine_cache(source, cache_dir)

with local_path.open() as f:
    rows = f.readlines()

Use the returned path for reading. If the original file changes later and its modification time no longer matches the cached copy, the next call refreshes the cache.

Development

pip install -r requirements_dev.txt
pip install -e .
make test
make lint
make docs

The test suite is configured with pytest; docs are built with Sphinx. Releases are versioned in both setup.py and scratchcache/__init__.py.

Changelog

0.0.1

  • First release on PyPI.

Release files for scratchcache 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scratchcache 0.0.2
File Size Uploaded
scratchcache-0.0.2.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scratchcache 0.0.2
File Interpreter ABI Platform
scratchcache-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:13.7 kB

Release files / scratchcache-0.0.2.tar.gz

Download URL scratchcache-0.0.2.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
427dbaa039bfdded9842deec95778ebfcc5231091ab2a74dd68dd360a07bbe58
BLAKE2b-256 checksum
How to use checksums
6509506ca0e89b5e4d278e7de2722197ae1b083fc0c020542cb9454068e49d85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / scratchcache-0.0.2-py2.py3-none-any.whl

Download URL scratchcache-0.0.2-py2.py3-none-any.whl
Size 4.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
433b43c3c7b359b827f1437ae397be1477454a1537314858d201e4e9b5cb896f
BLAKE2b-256 checksum
How to use checksums
b461ac7d202f4ad8da907910afb1dff10ac975c0d48762346068b9ac2a2ed84f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page