virgo777/buddyme/content-hash-cache-pattern
Claude Code and agent skill by virgo777/buddyme
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
使用 SHA-256 内容哈希缓存高昂的文件处理结果 —— 与路径无关、自动失效且服务层分离。
Install
npx vskill@latest install virgo777/buddyme/content-hash-cache-patternSKILL.md
The same SKILL.md was published earlier as xu-xiang/everything-claude-code-zh/content-hash-cache-pattern.
内容哈希文件缓存模式 (Content-Hash File Cache Pattern)
使用 SHA-256 内容哈希(而非文件路径)作为缓存键,缓存高昂的文件处理结果(如 PDF 解析、文本提取、图像分析)。与基于路径的缓存不同,这种方法在文件移动/重命名后依然有效,并在内容变化时自动失效。
何时激活
- 构建文件处理流水线(Pipeline),如 PDF、图像或文本提取
- 处理成本高昂且需要重复处理相同文件
- 需要提供
--cache/--no-cache命令行选项 - 希望在不修改现有纯函数(Pure Functions)的情况下为其添加缓存功能
核心模式
1. 基于内容哈希的缓存键
使用文件内容(而非路径)作为缓存键:
为什么使用内容哈希? 文件重命名/移动 = 缓存命中。内容更改 = 自动失效。无需索引文件。
2. 用于缓存条目的冻结数据类 (Frozen Dataclass)
3. 基于文件的缓存存储
每个缓存条目存储为 {hash}.json —— 通过哈希实现 O(1) 查询,无需索引文件。
4. 服务层封装 (满足单一职责原则 SRP)
保持处理函数为纯函数。将缓存逻辑作为独立的服务层添加。
关键设计决策
| 决策 | 理由 | |
|---|---|---|
| SHA-256 内容哈希 | 与路径无关,内容更改时自动失效 | |
{hash}.json 文件命名 | O(1) 查询速度,无需维护索引文件 | |
| 服务层封装 (Wrapper) | 满足单一职责原则(SRP):提取逻辑保持纯净,缓存作为独立关注点 | |
| 手动 JSON 序列化 | 对冻结数据类的序列化拥有完全控制权 | |
损坏返回 None | 优雅降级,在下次运行时重新处理 | |
cache_dir.mkdir(parents=True) | 在首次写入时延迟创建目录 |
最佳实践
- 哈希内容而非路径 —— 路径会变,内容身份(Identity)不变。
- 对大文件进行分块哈希 —— 避免将整个文件加载进内存。
- 保持处理函数纯净 —— 它们不应知道任何关于缓存的信息。
- 记录缓存命中/未命中日志 —— 使用截断后的哈希值以便调试。
- 优雅处理缓存损坏 —— 将无效的缓存条目视为未命中,绝不要因此崩溃。
应避免的反模式 (Anti-Patterns)
适用场景
- 文件处理流水线(PDF 解析、OCR、文本提取、图像分析)
- 受益于
--cache/--no-cache选项的命令行工具 (CLI) - 在不同运行周期中会出现相同文件的批处理任务
- 在不修改现有纯函数的情况下为其添加缓存功能
不适用场景
- 必须保持绝对实时的数据(实时数据流)
- 缓存条目体积极其庞大(此时应考虑流式处理)
- 结果取决于文件内容以外的参数(例如不同的提取配置)
Security
- Security scanPassed, 90 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 repositoryCheckingbuddyMe/skill_library/skills/content-hash-cache-pattern/SKILL.md
Popularity
- Repository stars
- 67
- Repository forks
- 8
- 7-day trend score
- 0
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/virgo777/buddyme/content-hash-cache-pattern)