| name | content-hash-cache-pattern |
| description | Cache expensive file processing results using SHA-256 content hashes — path-independent, auto-invalidating, with service layer separation. |
| source_path | skills/content-hash-cache-pattern/SKILL.md |
| origin | ECC |
Content-Hash File Cache Pattern
Cache expensive file processing results (PDF parsing, text extraction, image analysis) using SHA-256 content hashes as cache keys. Unlike path-based caching, this approach survives file moves/renames and auto-invalidates when content changes.
When to Activate
- Building file processing pipelines (PDF, images, text extraction)
- Processing cost is high and same files are processed repeatedly
- Need a
--cache/--no-cache CLI option
- Want to add caching to existing pure functions without modifying them
Core Pattern
1. Content-Hash Based Cache Key
Use file content (not path) as the cache key:
import hashlib
from pathlib import Path
_HASH_CHUNK_SIZE = 65536
def compute_file_hash(path: Path) -> str:
path.is_file():
FileNotFoundError()
sha256 = hashlib.sha256()
(path, ) f:
:
chunk = f.read(_HASH_CHUNK_SIZE)
chunk:
sha256.update(chunk)
sha256.hexdigest()