caching-strategy-optimizer
Caching Strategy Optimizer
Dramatically speed up CI pipelines with intelligent caching.
Cache Key Strategy
Package Manager Caches
# 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 Layer Caching
Using 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-based Cache
- 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
Build Output Caching
# 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') }}
Test Results Caching
# 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/After Metrics
## 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)**
Cache Hit Rate Monitoring
- 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
Best Practices
- Precise keys: Include all dependencies in hash
- Restore keys: Fallback to partial matches
- Multiple paths: Cache related files together
- Size limits: GitHub Actions limit is 10GB
- Expiration: Caches expire after 7 days
- Mode=max: Docker cache mode for better hits
- Monitor hits: Track cache effectiveness
Common Pitfalls
❌ Too generic keys: key: deps (always hits)
✅ Specific keys: key: deps-${{ hashFiles('package-lock.json') }}
❌ Missing restore-keys: Cache miss on minor changes ✅ Restore keys: Partial match fallback
❌ Caching node_modules with wrong lock file ✅ Match lock file: Hash the right lockfile
Output Checklist
- Package manager cache configured
- Build output cache
- Docker layer cache (if applicable)
- Test cache configured
- Cache keys use file hashes
- Restore keys for fallback
- Before/after metrics documented
- Cache hit monitoring
More from monkey1sai/openai-cli
multi-tenant-safety-checker
Ensures tenant isolation at query and policy level using Row Level Security, automated testing, and security audits. Prevents data leakage between tenants. Use for "multi-tenancy", "tenant isolation", "RLS", or "data security".
10modal-drawer-system
Implements accessible modals and drawers with focus trap, ESC to close, scroll lock, portal rendering, and ARIA attributes. Includes sample implementations for common use cases like edit forms, confirmations, and detail views. Use when building "modals", "dialogs", "drawers", "sidebars", or "overlays".
10eslint-prettier-config
Configures ESLint and Prettier for consistent code quality with TypeScript, React, and modern best practices. Use when users request "ESLint setup", "Prettier config", "linting configuration", "code formatting", or "lint rules".
9api-security-hardener
Hardens API security with rate limiting, input validation, authentication, and protection against common attacks. Use when users request "API security", "secure API", "rate limiting", "input validation", or "API protection".
9secure-headers-csp-builder
Implements security headers and Content Security Policy with safe rollout strategy (report-only → enforce), testing, and compatibility checks. Use for "security headers", "CSP", "HTTP headers", or "XSS protection".
9security-incident-playbook-generator
Creates response procedures for security incidents with containment steps, communication templates, and evidence collection. Use for "incident response", "security playbook", "breach response", or "IR plan".
9