content-hash-cache-pattern

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

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill content-hash-cache-pattern-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: content-hash-cache-pattern
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/content-hash-cache-pattern
Command: npx skills add https://github.com/Femad-6/my-skills --skill content-hash-cache-pattern-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Repeatedly processing the same files (PDF parsing, OCR, text extraction) wastes compute time, and path-based caches break whenever files are moved or renamed. This Skill provides a Python pattern that keys cache entries on file content, so renames still hit the cache and content edits automatically invalidate stale entries. ## Core Features & Use Cases - Content-Hash Cache Keys: Computes chunked SHA-256 hashes of file contents so cache identity survives moves and renames while auto-invalidating on edits. - File-Based Storage: Stores each result as {hash}.json for O(1) lookup with no index file, and treats corrupted entries as cache misses instead of crashing. - Service Layer Separation: Wraps pure extraction functions in a caching layer, keeping single-responsibility design and enabling a --cache/--no-cache CLI option. - Use Case: A batch pipeline that extracts text from hundreds of PDFs across nightly runs can skip re-processing unchanged files even after the folder structure is reorganized. ## Quick Start Add content-hash based caching to my PDF text extraction function so repeated runs skip unchanged files and support a --no-cache flag.

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 each result as a JSON file named by that hash. On subsequent runs, check for an existing entry before re-running the expensive processing function.

Why use content hash instead of file path for caching?

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

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

Read the file in fixed-size chunks, such as 64KB blocks, and feed each chunk into the hashlib SHA-256 object incrementally. This keeps memory usage constant regardless of file size.

What happens when a cache file is corrupted?

The pattern treats JSON decode errors, missing keys, and invalid values as cache misses and returns None. The processing function then re-runs and overwrites the bad entry, so corruption never crashes the pipeline.

When should I not use content-hash caching?

Avoid it for data that must always be fresh, such as real-time feeds, or when results depend on parameters beyond file content like different extraction configs. Extremely large cache entries may also be better handled with streaming.