content-hash-cache-pattern

使用SHA-256内容哈希缓存昂贵的文件处理结果——路径无关、自动失效、服务层分离。

Content Preview
---
name: content-hash-cache-pattern
description: 使用SHA-256内容哈希缓存昂贵的文件处理结果——路径无关、自动失效、服务层分离。
origin: ECC
---

# 内容哈希文件缓存模式

使用 SHA-256 内容哈希作为缓存键,缓存昂贵的文件处理结果(PDF 解析、文本提取、图像分析)。与基于路径的缓存不同,此方法在文件移动/重命名后仍然有效,并在内容更改时自动失效。

## 何时激活

* 构建文件处理管道时(PDF、图像、文本提取)
* 处理成本高且同一文件被重复处理时
* 需要一个 `--cache/--no-cache` CLI 选项时
* 希望在不修改现有纯函数的情况下为其添加缓存时

## 核心模式

### 1. 基于内容哈希的缓存键

使用文件内容(而非路径)作为缓存键:

```python
import hashlib
from pathlib import Path

_HASH_CHUNK_SIZE = 65536  # 64KB chunks for large files

def compute
How to Use

Recommended: Install to project (local)

mkdir -p .claude/skills
curl -o .claude/skills/content-hash-cache-pattern.md \
  https://raw.githubusercontent.com/affaan-m/everything-claude-code/main/docs/zh-CN/skills/content-hash-cache-pattern/SKILL.md

Skill is scoped to this project only. Add .claude/skills/ to your .gitignoreif you don't want to commit it.

Alternative: Clone full repo

git clone https://github.com/affaan-m/everything-claude-code

Then reference at docs/zh-CN/skills/content-hash-cache-pattern/SKILL.md

Related Skills