| name | caching-optimizer |
| description | Optimize caching strategies for performance. Use when analyzing cache effectiveness, implementing multi-layer caching, optimizing Redis/memory caches, or troubleshooting slow response times. |
Caching Optimizer
Intelligent caching strategy optimization for applications and APIs
Quick Commands
# Analyze cache effectiveness
npx @j0kz/caching-optimizer analyze
# Generate caching strategy
npx @j0kz/caching-optimizer suggest --profile=api
# Clear and warm cache
npm run cache:clear && npm run cache:warm
# Monitor cache metrics
npx @j0kz/caching-optimizer monitor
Core Functionality
Key Features
- Cache Analysis: Hit/miss ratios and effectiveness
- Strategy Optimization: LRU, LFU, TTL recommendations
- Multi-Layer Caching: Memory, Redis, CDN coordination
- Cache Warming: Preload frequently accessed data
- Invalidation Strategies: Smart cache busting
Detailed Information
For comprehensive details, see:
cat .claude/skills/caching-optimizer/references/caching-patterns.md
cat .claude/skills/caching-optimizer/references/redis-optimization.md
cat .claude/skills/caching-optimizer/references/cdn-strategies.md
Usage Examples
Example 1: Implement Multi-Layer Cache
import { CachingOptimizer } from '@j0kz/caching-optimizer';
const optimizer = new CachingOptimizer({
layers: [
{ type: 'memory', size: '100MB', ttl: 300 },
{ type: 'redis', size: '1GB', ttl: 3600 },
{ type: 'cdn', ttl: 86400 }
]
});
// Automatic layer selection
const data = await optimizer.get('user:123', async () => {
return await fetchUserFromDB(123);
});
Example 2: Cache Effectiveness Analysis
const metrics = await optimizer.analyze({
period: '24h',
breakdown: true
});
console.log(`Hit Rate: ${metrics.hitRate}%`);
console.log(`Miss Rate: ${metrics.missRate}%`);
console.log(`Eviction Rate: ${metrics.evictionRate}%`);
// Get recommendations
const suggestions = optimizer.suggest(metrics);
suggestions.forEach(s => console.log(s));
Caching Strategies
Common Patterns
Cache-Aside (Lazy Loading)
const data = cache.get(key) || await loadAndCache(key);Write-Through
await Promise.all([ cache.set(key, data), database.save(key, data) ]);Write-Behind (Write-Back)
cache.set(key, data); queue.push({ action: 'save', key, data });
Cache Keys Strategy
// Hierarchical keys for batch invalidation
const keyPatterns = {
user: 'user:{id}',
userPosts: 'user:{userId}:posts',
post: 'post:{id}',
postComments: 'post:{postId}:comments:{page}'
};
Configuration
{
"caching-optimizer": {
"default": {
"strategy": "lru",
"maxSize": "512MB",
"ttl": 3600,
"compression": true
},
"layers": {
"memory": {
"enabled": true,
"maxSize": "128MB"
},
"redis": {
"enabled": true,
"cluster": false,
"db": 0
},
"cdn": {
"enabled": true,
"provider": "cloudflare"
}
},
"monitoring": {
"metrics": ["hitRate", "missRate", "latency"],
"alertThreshold": {
"hitRate": 0.8,
"latency": 100
}
}
}
}
Cache Invalidation
// Tag-based invalidation
await optimizer.tag(['user:123', 'posts']).set(key, data);
await optimizer.invalidateTag('user:123');
// Pattern-based invalidation
await optimizer.invalidatePattern('user:123:*');
// Time-based invalidation
await optimizer.set(key, data, { ttl: 3600, slide: true });
Notes
- Supports Redis, Memcached, and in-memory caching
- Automatic cache stampede prevention
- Compression for large values
- Distributed caching support for microservices