content-hash-cache-pattern
यह क्या करती है
यह pattern बार-बार होने वाली file processing को तेज करता है, जैसे PDF से text निकालना या images का विश्लेषण करना। यह file के content से बने एक अनोखे code का इस्तेमाल करके नतीजे cache (तेज memory) में सेव करता है। अगर file नहीं बदली है, तो नतीजा cache से तुरंत मिल जाता है।
कब इस्तेमाल करें
- ✓जब आप एक ही files को कई बार process करते हैं और इसमें बहुत समय लगता है।
- ✓PDF या images जैसी बहुत सारी files संभालने वाले systems बनाते समय।
- ✓जब चाहते हैं कि files के इधर-उधर होने या नाम बदलने पर भी cache काम करता रहे।
Skill का लिंक
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/content-hash-cache-pattern/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“क्या इस PDF report से text निकालने की गति बढ़ाने के लिए cache इस्तेमाल कर सकता हूं?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
यह file के बिल्कुल सटीक content के आधार पर एक अनोखा code (hash) बनाता है। इस code से cache file में सेव किया गया नतीजा ढूंढा जाता है। अगर file का content बदलता है, तो नया code बनता है और पुराना नतीजा अनदेखा कर दिया जाता है।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
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-cacheCLI 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
| Decision | Rationale |
|---|---|
| SHA-256 content hash | Path-independent, auto-invalidates on content change |
{hash}.json file naming |
O(1) lookup, no index file needed |
| Service layer wrapper | SRP: extraction stays pure, cache is a separate concern |
| Manual JSON serialization | Full control over frozen dataclass serialization |
Corruption returns None |
Graceful 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}
…
अक्सर पूछे जाने वाले सवाल
file के नाम की जगह उसका content क्यों इस्तेमाल करें?
क्योंकि file का नाम बदला या स्थानांतरित किया जा सकता है, लेकिन उसका content वही रह सकता है। content का इस्तेमाल करने से ऐसी स्थितियों में भी cache सही तरीके से काम करता है।
अगर file का content बदल जाए तो क्या होगा?
नए content के लिए एक नया अनोखा code (hash) बनता है। system समझ जाता है कि पुराना cached नतीजा अब मान्य नहीं है, और file को दोबारा process करके नया cached नतीजा बनाता है।
नतीजे कहां सेव होते हैं?
नतीजे अलग-अलग files में सेव होते हैं, जिनके नाम content के अनोखे code (hash) पर रखे जाते हैं। इससे system बिना किसी अलग index के सही नतीजा तुरंत ढूंढ लेता है।
जानकारी
लेखक का मूल विवरण
Cache expensive file processing results using SHA-256 content hashes — path-independent, auto-invalidating, with service layer separation. Use when repeated file processing is slow and results should be cached and invalidated by content rather than path.
repo: affaan-m/ECC
लोकप्रियता
- GitHub स्टार
- 280k
- रैंकिंग में स्थान
- #153
- लाइसेंस
- MIT
- अपडेट की गई
- 15/10/2026
कमेंट
- …
इस repository की और skills
accessibility
280kयह आपके एजेंट को ऐसे इंटरफ़ेस बनाने में मदद करता है जिन्हें हर कोई इस्तेमाल कर सके। यह पक्का करता है कि वेबसाइट और ऐप दिव्यांग लोगों के लिए भी अच्छे से काम करें, जैसे नेत्रहीन या जिन्हें चलने-फिरने में दिक्कत होती है।
agent-architecture-audit
280kयह आपके एजेंट को जाँचने देता है कि सब कुछ सही से चल रहा है। यह AI सिस्टम में समस्याएँ ढूँढकर ठीक करता है, ताकि वे उम्मीद के मुताबिक काम करें।
agent-eval
280kयह आपके एजेंट को अलग-अलग AI coding tools की तुलना करने में मदद करता है। यह मापता है कि किसी खास काम के लिए कौन सा टूल ज़्यादा तेज़, सस्ता और भरोसेमंद है।
agent-harness-construction
280kयह आपके एजेंट को काम कैसे करना है, यह बेहतर समझने में मदद करता है। यह उन tools और instructions को बेहतर बनाता है जिनका एजेंट इस्तेमाल करता है, ताकि उसकी सफलता की संभावना बढ़े।
मिलती-जुलती skills
knowledge-automation
394kयह आपके एजेंट को एक नियमित काम सेट करने देती है जो समय-सारणी पर Knowledge vault में नई सामग्री जोड़ता है, फिर जांचता है कि काम चल रहा है।
knowledge-source-ingest
394kयह आपके एजेंट को चैट चैनल, फ़ीड या मेलबॉक्स से आई नई चीज़ों से Knowledge vault अपडेट रखने देती है, बिना वह दोबारा जोड़े जो पहले से है।
watchers
260kआपके एजेंट को RSS फ़ीड, JSON endpoints या GitHub गतिविधि को तय समय पर देखने देती है, और केवल पिछली जांच के बाद नए आइटम की सूचना देती है।