content-hash-cache-pattern

Cache file processing results using SHA-256 content hashes as keys.

5|Updated Jul 8, 2019
One-click install
npx skills add https://github.com/rinchsan/dotfiles --skill content-hash-cache-pattern-rinchsan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: content-hash-cache-pattern
Source: https://github.com/rinchsan/dotfiles/tree/main/.claude/skills/content-hash-cache-pattern
Command: npx skills add https://github.com/rinchsan/dotfiles --skill content-hash-cache-pattern-rinchsan

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Re-running expensive file processing (PDF parsing, OCR, text extraction) on unchanged files wastes time and compute, while path-based caches break whenever files are moved or renamed. ## Core Features & Use Cases - Content-Hash Cache Keys: Uses SHA-256 of file contents so cache survives renames and auto-invalidates on content changes. - Service Layer Separation: Keeps extraction functions pure while a wrapper handles cache lookup, storage, and logging. - Graceful Degradation: Corrupted cache entries are treated as misses and reprocessed instead of crashing. - Use Case: A CLI tool that extracts text from hundreds of PDFs can add a --cache/--no-cache flag so repeated runs skip already-processed files instantly. ## Quick Start Add content-hash based caching to my Python file extraction pipeline so repeated runs skip unchanged files.

Frequently Asked Questions about content-hash-cache-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I cache file processing results in Python?

Compute a SHA-256 hash of the file contents and use it as the cache key, storing results as {hash}.json files. Check the cache before processing and write results after a miss, keeping the processing function itself pure.

Why use content hash instead of file path for caching?

Content-hash caching survives file moves and renames because the key depends on file contents, not location. It also auto-invalidates when content changes, eliminating stale cache entries without an index file.

How do I hash large files without loading them into memory?

Read the file in chunks (e.g., 64KB) and feed each chunk to hashlib.sha256() incrementally. This keeps memory usage constant regardless of file size while producing the same digest.

What happens when a cache file is corrupted?

Treat corrupted cache entries as cache misses by catching JSONDecodeError, ValueError, and KeyError during reads. The file is then reprocessed and the cache rewritten, so corruption never crashes the pipeline.

When should I not use content-hash caching?

Avoid it for data that must always be fresh, extremely large cache entries better handled by streaming, or results that depend on parameters beyond file content such as different extraction configurations.