Back to Skills

content-hash-cache-pattern

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

176,164stars27,258forksUpdated 5/9/2026

Security Assessment

Safe(100/100)
Security Score100/100

About content-hash-cache-pattern

The content-hash-cache-pattern skill provides a mechanism for caching the results of computationally expensive file processing tasks using SHA-256 hashes of file contents as cache keys. Unlike traditional path-based caching, this method ensures that the cache remains valid even if files are moved or renamed, and automatically invalidates entries when the file content changes. By decoupling the caching logic from the core processing functions, it allows developers to maintain pure functions while adding caching as a separate service layer, improving efficiency without introducing side effects or modifying existing code.

This skill features a content-hash-based cache key computation for reliable cache identification, a frozen dataclass structure to represent cache entries, and file-based storage where each cache entry is saved as a JSON file named after the content hash. The service layer wrapper handles cache checks, extraction of data, and writing back to the cache, preserving the single-responsibility principle. Additional features include chunked SHA-256 computation for large files, automatic cache invalidation on content changes, and safe handling of corrupted cache files by treating them as cache misses.

Typical use cases include file processing pipelines for PDFs, images, or text extraction, where repeated processing of the same files is costly. It is especially useful for developers implementing command-line interfaces with optional caching, or for enhancing existing pure functions with caching capabilities without changing their core logic. Target users are Python developers, data engineers, and researchers who need efficient and reliable caching for file-based workflows, particularly in scenarios involving large datasets or repeated data processing operations.

FAQ

How do I enable or disable caching when using this pattern?

Caching can be enabled or disabled via the service layer wrapper using the 'cache_enabled' parameter. Setting it to True enables caching, while False bypasses the cache entirely.

Does the cache remain valid if files are renamed or moved?

Yes, because the cache uses the SHA-256 hash of file contents rather than the file path, renaming or moving the file does not affect cache hits.

What happens if a cache file is corrupted?

If a cached JSON file is corrupted or unreadable, the system treats it as a cache miss and regenerates the cached content during the next extraction.

Can this pattern handle large files efficiently?

Yes, the SHA-256 computation is performed in chunks (64KB by default) to efficiently handle large files without loading them entirely into memory.

Is this caching pattern specific to any file type?

No, it can be applied to any file type as long as the content can be processed and a SHA-256 hash can be computed.

Install content-hash-cache-pattern

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill