关于 caching-strategy-optimizer
用于规划 CI 缓存的 Skill,覆盖依赖安装、构建、Docker 层和测试。它提供缓存键与恢复键指引,并列出命中率监控及优化前后指标检查项。
主要功能
- 01配置包管理器缓存
- 02缓存 Docker 层与构建产物
- 03缓存测试结果
- 04缓存键与恢复键指引
- 05命中率监控与指标
适用场景
- 01减少 CI 中重复安装依赖
- 02缓存 Docker 构建和应用产物
- 03跟踪流水线运行中的缓存效果
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ patricio0312rev/ skills --skill caching-strategy-optimizer安装后在 Claude Code 中输入/caching-strategy-optimizer确认已加载。
详细介绍
缓存策略优化器
借助智能缓存,大幅加快 CI 流水线。
缓存键策略
包管理器缓存
# NPM - Hash package-lock.json
- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
# pnpm - More aggressive caching
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/cache@v3
with:
path: |
~/.pnpm-store
node_modules
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
# Python pip
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
# Cargo/Rust
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
Docker 层缓存
使用 Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build with cache
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
基于 Registry 的缓存
- name: Build with registry cache
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=registry,ref=myapp:buildcache
cache-to: type=registry,ref=myapp:buildcache,mode=max
构建输出缓存
# Next.js cache
- uses: actions/cache@v3
with:
path: |
${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
${{ runner.os }}-nextjs-
# Webpack cache
- uses: actions/cache@v3
with:
path: node_modules/.cache/webpack
key: ${{ runner.os }}-webpack-${{ hashFiles('webpack.config.js') }}-${{ hashFiles('src/**') }}
# TypeScript build cache
- uses: actions/cache@v3
with:
path: |
dist
tsconfig.tsbuildinfo
key: ${{ runner.os }}-tsc-${{ hashFiles('**/*.ts') }}
测试结果缓存
# Jest cache
- uses: actions/cache@v3
with:
path: /tmp/jest_rt
key: ${{ runner.os }}-jest-${{ hashFiles('**/*.test.ts') }}
# Pytest cache
- uses: actions/cache@v3
with:
path: .pytest_cache
key: ${{ runner.os }}-pytest-${{ hashFiles('**/*test*.py') }}
优化前后指标
## Before Optimization
- Total time: 12 minutes
- npm ci: 4 minutes
- Build: 5 minutes
- Tests: 3 minutes
## After Caching
- Total time: 3 minutes
- npm ci: 30 seconds (cache hit)
- Build: 1 minute (incremental)
- Tests: 1.5 minutes (cache hit)
**Improvement: 75% faster (12m → 3m)**
缓存命中率监控
- name: Check cache hit
id: cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-deps-${{ hashFiles('package-lock.json') }}
- name: Log cache status
run: |
if [ "${{ steps.cache.outputs.cache-hit }}" == "true" ]; then
echo "✅ Cache hit - saved $(date -u -d @$SECONDS +%M:%S)"
else
echo "❌ Cache miss - installing from scratch"
fi
最佳实践
- 精确的键:将所有依赖项纳入哈希
- 恢复键:回退到部分匹配
- 多个路径:将相关文件一起缓存
- 大小限制:GitHub Actions 的限制为 10GB
- 过期时间:缓存会在 7 天后过期
- Mode=max:使用 Docker 缓存模式以获得更高的命中率
- 监控命中情况:跟踪缓存效果
常见陷阱
❌ 键过于宽泛:key: deps(始终命中)
✅ 指定具体键:key: deps-${{ hashFiles('package-lock.json') }}
❌ 缺少 restore-keys:轻微变更也会导致缓存未命中 ✅ 恢复键:提供部分匹配回退
❌ 使用错误的锁文件缓存 node_modules ✅ 匹配锁文件:对正确的锁文件计算哈希
输出检查清单
- 已配置包管理器缓存
- 已配置构建输出缓存
- 已配置 Docker 层缓存(如适用)
- 已配置测试缓存
- 缓存键使用文件哈希
- 已设置用于回退的恢复键
- 已记录优化前后指标
- 已监控缓存命中情况
评论
支持 Markdown