shennawardana23/skillme/content-hash-cache-pattern
Claude Code and agent skill by shennawardana23/skillme
- community
Checking that the SKILL.md file is still in the repository.
Cache expensive file-processing results (PDF parsing, OCR, text extraction, image analysis) keyed by a SHA-256 hash of file content rather than file path, so renamed files still hit the cache and changed content auto-invalidates it. Use when building a file-processing pipeline that reprocesses the same files across runs, or when adding a --cache/--no-cache option without touching the existing pure processing function.
Install
npx vskill@latest install shennawardana23/skillme/content-hash-cache-patternSKILL.md
The same SKILL.md was published earlier as shennawardana23/skillme/content-hash-cache-pattern.
Content-Hash File Cache Pattern
Cache expensive file-processing results using a SHA-256 hash of the file's content as the cache key. Unlike path-based caching, this survives file moves and renames, and auto-invalidates the moment content changes — no separate invalidation logic needed.
When to Activate
- Building a file-processing pipeline (PDF parsing, OCR, text extraction, image analysis) where the same files reappear across runs
- Processing cost is high enough that reprocessing unchanged files is wasteful
- Adding a
--cache/--no-cacheflag to a CLI tool - Retrofitting caching onto an existing pure function without modifying that function
Core Pattern (Go)
1. Content hash as the cache key
Hash the file in chunks — never load the whole file into memory:
io.Copy streams the file into the hasher in fixed-size buffers, so this scales to large files without loading them fully into memory.
2. Cache entry type
3. File-based storage: {hash}.json
O(1) lookup by hash, no separate index file:
4. Service-layer wrapper (single responsibility)
Keep the processing function pure. Caching is a separate layer that wraps it:
Key Design Decisions
| Decision | Rationale | |
|---|---|---|
| SHA-256 content hash, not path | Path-independent; auto-invalidates the instant content changes | |
{hash}.json file naming | O(1) lookup, no index file to keep in sync | |
| Service layer wraps a pure function | Extraction logic stays testable and reusable; caching is orthogonal | |
| Corrupted/missing cache file → cache miss | Graceful degradation: reprocess rather than crash | |
| Cache write failure → log, don't fail the call | The correct result still reaches the caller even if persistence fails | |
Lazy MkdirAll on first write | No setup step required before first use |
Best Practices
- Hash content, not paths — paths change identity, content doesn't.
- Stream large files into the hasher (
io.Copy) instead of reading them fully into memory first. - Keep the processing function pure — it should have zero knowledge that caching exists.
- Log cache hit/miss with a truncated hash prefix for debugging without flooding logs.
- Treat any cache-read error (missing file, bad JSON, wrong shape) as a miss, never as a fatal error.
Gotchas
- Two files with identical content but different intended outputs (e.g. same PDF processed with two different extraction configs) will collide on the same cache key if the config isn't part of what's hashed — if extraction behavior depends on parameters beyond file bytes, fold those parameters into the key (e.g. hash the file content concatenated with a serialized config) or this pattern silently returns the wrong result for the second config.
- A cache entry for a very large result (e.g. full-page OCR output for a 500-page PDF) written as a single JSON file works but doesn't scale indefinitely — past some size, prefer streaming the cached artifact to disk directly rather than round-tripping it through JSON.
- This pattern is wrong for anything that must always be fresh (a live price feed, a real-time API response) — the entire point of the pattern is durable reuse of a result tied to unchanging content, which doesn't apply to data that's expected to change independent of any file.
Anti-Patterns to Avoid
Real-world grounding
Git's object model uses exactly this idea at the core of the entire system: every blob, tree, and commit is addressed by the SHA hash of its own content, not by a file path. That's precisely why renaming a file with unchanged content doesn't create a new object in Git's store — the object is identified by what it contains, not where it lives. This pattern applies the same principle to a processing cache instead of a version-control object store.
Verification
- [ ] The cache key is a hash of file content, never the file path
- [ ] Large files are hashed via streaming (
io.Copy), not loaded fully into memory - [ ] The processing function has no cache-related parameters or branches
- [ ] A missing or corrupt cache file degrades to a cache miss, not a crash
- [ ] Parameters that affect output (beyond file bytes) are folded into the cache key if they vary
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/content-hash-cache-pattern/SKILL.md
Popularity
- Repository stars
- 2
- 7-day trend score
- 5
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/shennawardana23/skillme/content-hash-cache-pattern)