Skip to main content

Scratch Cache

Project description

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.

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

scratchcache-0.0.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

scratchcache-0.0.2-py2.py3-none-any.whl (4.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file scratchcache-0.0.2.tar.gz.

File metadata

  • Download URL: scratchcache-0.0.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for scratchcache-0.0.2.tar.gz
Algorithm Hash digest
SHA256 427dbaa039bfdded9842deec95778ebfcc5231091ab2a74dd68dd360a07bbe58
MD5 6f91abbd260a7446902a785458cc2073
BLAKE2b-256 6509506ca0e89b5e4d278e7de2722197ae1b083fc0c020542cb9454068e49d85

See more details on using hashes here.

File details

Details for the file scratchcache-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: scratchcache-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for scratchcache-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 433b43c3c7b359b827f1437ae397be1477454a1537314858d201e4e9b5cb896f
MD5 54489e5e79df9e5f7ca074369da975cc
BLAKE2b-256 b461ac7d202f4ad8da907910afb1dff10ac975c0d48762346068b9ac2a2ed84f

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