cache-strategy
FreeNo executable scriptsNot checkedDesign and implement caching layers for APIs and web applications using Redis or Memcached. Use when you need to reduce database load, improve response times…
by TerminalSkills★ 221 GitHub starsAbout this skill
Cache Strategy
Overview
This skill helps you design and implement multi-layer caching strategies for high-traffic APIs. It covers choosing the right caching pattern for your data access profile, configuring TTLs, preventing cache stampedes, and setting up cache invalidation that actually works in production.
Instructions
1. Analyze the caching opportunity
Before adding caching, identify what to cache by examining query patterns:
// Instrument your API routes to log response times and call frequency
// Look for: high frequency + low change rate = best cache candidates
// Example analysis output:
// GET /api/products → 12,000 req/min, changes every 30min → CACHE (TTL: 5min)
// GET /api/products/:id → 8,000 req/min, changes on update → CACHE (invalidate on write)
// POST /api/orders → 200 req/min, always unique → DO NOT CACHE
// GET /api/user/profile → 3,000 req/min, changes rarely → CACHE (TTL: 15min)
2. Implement cache-aside pattern (most common)
The application checks cache first, falls back to database, then populates cache:
import { Redis } from "ioredis"; // ioredis 6: named import is the documented TypeScript form
// ioredis queues commands while disconnected; maxRetriesPerRequest makes them fail
// fast so a Redis outage falls through to the database instead of hanging requests.
const redis = new Redis({ host: "localhost", port: 6379, maxRetriesPerRequest: 3 });
async function getCached<T>(
key: string,
fetcher: () => Promise<T>,
ttlSeconds: number = 300
): Promise<T> {
try {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
} catch (err) {
console.warn(`cache read failed for ${key}, going to the database`, err);
return fetcher();
}
const data = await fetcher();
// A failed write must not fail the request; the next read simply misses again.
await redis.set(key, JSON.stringify(data), "EX", ttlSeconds).catch(() => undefined);
return data;
}
// Usage in route handler
app.get("/api/products/:id", async (req, res) => {
const product = await getCached(
`product:${req.params.id}`,
() => db.products.findById(req.params.id),
600 // 10 minutes
);
res.json(product);
});
3. Prevent cache stampedes
When a popular key expires, hundreds of requests hit the database simultaneously:
async function getCachedWithLock<T>(
key: string,
fetcher: () => Promise<T>,
ttlSeconds: number = 300
): Promise<T> {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const lockKey = `lock:${key}`;
const acquired = await redis.set(lockKey, "1", "EX", 10, "NX");
if (acquired) {
try {
const data = await fetcher();
await redis.set(key, JSON.stringify(data), "EX", ttlSeconds);
return data;
} finally {
await redis.del(lockKey);
}
}
// Another process is refreshing — wait and retry
await new Promise((r) => setTimeout(r, 100));
return getCachedWithLock(key, fetcher, ttlSeconds);
}
4. Cache invalidation strategies
Pattern-based invalidation for related data:
async function invalidatePattern(pattern: string): Promise<void> {
let cursor = "0";
do {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
if (keys.length > 0) await redis.del(...keys);
cursor = nextCursor;
} while (cursor !== "0");
}
// On product update: invalidate product and any list containing it
async function onProductUpdate(productId: string): Promise<void> {
await redis.del(`product:${productId}`);
await invalidatePattern("products:list:*");
}
Examples
Example 1: API response caching
Prompt: "Add Redis caching to my Express API. The /api/catalog endpoint is called 15,000 times per minute but the data only changes when an admin publishes updates. I want sub-10ms response times."
Agent output:
- Adds
src/cache/redis-client.tswith connection pooling and retry config - Wraps
/api/catalogwith cache-aside pattern, 5-minute TTL - Adds stampede prevention with distributed locking
- Creates
src/cache/invalidation.ts— called from the admin publish endpoint to bust catalog cache - Adds cache hit/miss metrics via response headers (
X-Cache: HIT/X-Cache: MISS)
Example 2: Multi-layer caching
Prompt: "Our product API serves 50,000 RPM. Add in-memory cache for the hottest 1,000 items and Redis for everything else. Products change when inventory updates."
Agent output:
- Adds
src/cache/memory-lru.tsusing an LRU cache with 1,000 max entries and 60-second TTL - Adds
src/cache/tiered-cache.tsthat checks memory → Redis → database in sequence - Creates
src/events/inventory-handler.tsthat invalidates both cache layers on inventory change - Adds
/admin/cache/statsendpoint showing hit rates for each layer
Guidelines
- Cache-aside is the default — use write-through only when you need
Install cache-strategy in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
FAQ
What does the cache-strategy skill do?
Design and implement caching layers for APIs and web applications using Redis or Memcached. Use when you need to reduce database load, improve response times, or handle traffic spikes. Covers cache-aside, write-through, and write-behind patterns, TTL strategies, cache invalidation, and stampede prevention. Trigger words: cache, Redis, Memcached, TTL, cache invalidation, response time, throughput, rate limiting.
How do I install the cache-strategy skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the cache-strategy skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
by Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
by Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
by Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
by Anthropic