PluginBench
Skill
Review
Audit score 70

content-hash-cache-pattern

affaan-m/everything-claude-code

Cache expensive file processing results using SHA-256 content hashes — path-independent, auto-invalidating.

What is content-hash-cache-pattern?

Implements a content-hash-based caching pattern for file processing pipelines. Uses SHA-256 hashes of file contents as cache keys, enabling cache hits across file moves/renames and automatic invalidation on content changes. Separates caching logic into a service layer, keeping processing functions pure.

  • Compute SHA-256 hashes of file contents in chunks to avoid memory overhead
  • Store cache entries as individual JSON files named by hash for O(1) lookup
  • Wrap processing functions with a service layer that handles cache checks and writes
  • Gracefully degrade on cache corruption by treating invalid entries as cache misses
  • Support --cache/--no-cache CLI options without modifying core processing logic

How to install content-hash-cache-pattern

npx skills add https://github.com/affaan-m/everything-claude-code --skill content-hash-cache-pattern
Claude Code
Cursor
Windsurf
Cline

How to use content-hash-cache-pattern

  1. 1.Define a frozen dataclass for your cache entry (file_hash, source_path, and the cached result)
  2. 2.Implement compute_file_hash() using SHA-256 with chunked reading for large files
  3. 3.Create read_cache() and write_cache() functions using {hash}.json file naming
  4. 4.Keep your processing function (e.g., extract_text) pure with no cache awareness
  5. 5.Wrap it in a service layer function (e.g., extract_with_cache) that checks cache, processes on miss, and writes results
  6. 6.Add cache_enabled and cache_dir parameters to control caching behavior from CLI or callers

Use cases

Good for
  • PDF parsing pipelines where the same documents are processed repeatedly across runs
  • Batch image analysis workflows that benefit from caching extracted features
  • Text extraction tools with a --cache flag to skip re-processing unchanged files
  • CLI utilities processing large file collections where some files may be moved or renamed
  • Adding caching to existing pure extraction functions without refactoring them
Who it's for
  • Backend engineers building file processing pipelines
  • CLI tool developers who need caching without modifying core logic
  • Data processing teams handling repeated batch jobs on large file collections
  • Developers working with expensive operations like PDF parsing or image analysis

content-hash-cache-pattern FAQ

Why use content hash instead of file path as the cache key?

Content hashes are path-independent, so cache hits survive file moves and renames. They also auto-invalidate when file content changes, eliminating stale cache issues.

How do you handle very large files without loading them entirely into memory?

Read files in 64KB chunks when computing the SHA-256 hash. This keeps memory usage constant regardless of file size.

What happens if a cache file becomes corrupted?

The read_cache() function catches JSON decode errors and returns None, treating corruption as a cache miss. The file is re-processed on the next run.

Can I use this pattern if processing results depend on parameters other than file content?

This pattern works best when results depend only on file content. If results vary by extraction config or other parameters, you'd need to include those in the cache key or use a different caching strategy.

Do I need to modify my existing processing functions to add caching?

No. Keep your processing functions pure and unaware of caching. Add caching as a separate service layer wrapper that calls the pure function on cache misses.

Full instructions (SKILL.md)

Source of truth, from affaan-m/everything-claude-code.


name: content-hash-cache-pattern description: Cache expensive file processing results using SHA-256 content hashes — path-independent, auto-invalidating, with service layer separation. metadata: 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  # 64KB chunks for large files

def compute_file_hash(path: Path) -> str:
    """SHA-256 of file contents (chunked for large files)."""
    if not path.is_file():
        raise FileNotFoundError(f"File not found: {path}")
    sha256 = hashlib.sha256()
    with open(path, "rb") as f:
        while True:
            chunk = f.read(_HASH_CHUNK_SIZE)
            if not chunk:
                break
            sha256.update(chunk)
    return sha256.hexdigest()

Why content hash? File rename/move = cache hit. Content change = automatic invalidation. No index file needed.

2. Frozen Dataclass for Cache Entry

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class CacheEntry:
    file_hash: str
    source_path: str
    document: ExtractedDocument  # The cached result

3. File-Based Cache Storage

Each cache entry is stored as {hash}.json — O(1) lookup by hash, no index file required.

import json
from typing import Any

def write_cache(cache_dir: Path, entry: CacheEntry) -> None:
    cache_dir.mkdir(parents=True, exist_ok=True)
    cache_file = cache_dir / f"{entry.file_hash}.json"
    data = serialize_entry(entry)
    cache_file.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")

def read_cache(cache_dir: Path, file_hash: str) -> CacheEntry | None:
    cache_file = cache_dir / f"{file_hash}.json"
    if not cache_file.is_file():
        return None
    try:
        raw = cache_file.read_text(encoding="utf-8")
        data = json.loads(raw)
        return deserialize_entry(data)
    except (json.JSONDecodeError, ValueError, KeyError):
        return None  # Treat corruption as cache miss

4. Service Layer Wrapper (SRP)

Keep the processing function pure. Add caching as a separate service layer.

def extract_with_cache(
    file_path: Path,
    *,
    cache_enabled: bool = True,
    cache_dir: Path = Path(".cache"),
) -> ExtractedDocument:
    """Service layer: cache check -> extraction -> cache write."""
    if not cache_enabled:
        return extract_text(file_path)  # Pure function, no cache knowledge

    file_hash = compute_file_hash(file_path)

    # Check cache
    cached = read_cache(cache_dir, file_hash)
    if cached is not None:
        logger.info("Cache hit: %s (hash=%s)", file_path.name, file_hash[:12])
        return cached.document

    # Cache miss -> extract -> store
    logger.info("Cache miss: %s (hash=%s)", file_path.name, file_hash[:12])
    doc = extract_text(file_path)
    entry = CacheEntry(file_hash=file_hash, source_path=str(file_path), document=doc)
    write_cache(cache_dir, entry)
    return doc

Key Design Decisions

DecisionRationale
SHA-256 content hashPath-independent, auto-invalidates on content change
{hash}.json file namingO(1) lookup, no index file needed
Service layer wrapperSRP: extraction stays pure, cache is a separate concern
Manual JSON serializationFull control over frozen dataclass serialization
Corruption returns NoneGraceful degradation, re-processes on next run
cache_dir.mkdir(parents=True)Lazy directory creation on first write

Best Practices

  • Hash content, not paths — paths change, content identity doesn't
  • Chunk large files when hashing — avoid loading entire files into memory
  • Keep processing functions pure — they should know nothing about caching
  • Log cache hit/miss with truncated hashes for debugging
  • Handle corruption gracefully — treat invalid cache entries as misses, never crash

Anti-Patterns to Avoid

# BAD: Path-based caching (breaks on file move/rename)
cache = {"/path/to/file.pdf": result}

# BAD: Adding cache logic inside the processing function (SRP violation)
def extract_text(path, *, cache_enabled=False, cache_dir=None):
    if cache_enabled:  # Now this function has two responsibilities
        ...

# BAD: Using dataclasses.asdict() with nested frozen dataclasses
# (can cause issues with complex nested types)
data = dataclasses.asdict(entry)  # Use manual serialization instead

When to Use

  • File processing pipelines (PDF parsing, OCR, text extraction, image analysis)
  • CLI tools that benefit from --cache/--no-cache options
  • Batch processing where the same files appear across runs
  • Adding caching to existing pure functions without modifying them

When NOT to Use

  • Data that must always be fresh (real-time feeds)
  • Cache entries that would be extremely large (consider streaming instead)
  • Results that depend on parameters beyond file content (e.g., different extraction configs)