cache-strategy
RedisやMemcachedなどの技術を用いて、APIやWebアプリケーションの応答速度向上や負荷軽減のために、キャッシュ戦略の設計・実装を行い、様々なパターンや有効期限、無効化、同時アクセス対策などを考慮した最適なキャッシュシステムを構築するSkill。
📜 元の英語説明(参考)
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.
🇯🇵 日本人クリエイター向け解説
RedisやMemcachedなどの技術を用いて、APIやWebアプリケーションの応答速度向上や負荷軽減のために、キャッシュ戦略の設計・実装を行い、様々なパターンや有効期限、無効化、同時アクセス対策などを考慮した最適なキャッシュシステムを構築するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o cache-strategy.zip https://jpskill.com/download/14713.zip && unzip -o cache-strategy.zip && rm cache-strategy.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/14713.zip -OutFile "$d\cache-strategy.zip"; Expand-Archive "$d\cache-strategy.zip" -DestinationPath $d -Force; ri "$d\cache-strategy.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
cache-strategy.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
cache-strategyフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
キャッシュ戦略
概要
このスキルは、高トラフィック API 向けの多層キャッシュ戦略の設計と実装を支援します。データアクセスプロファイルに適したキャッシュパターンを選択し、TTL を設定し、キャッシュスタンピードを防止し、実際に本番環境で機能するキャッシュ無効化を設定する方法について説明します。
手順
1. キャッシュの機会を分析する
キャッシュを追加する前に、クエリパターンを調べて何をキャッシュするかを特定します。
// API ルートを計測して、応答時間と呼び出し頻度をログに記録します
// 高頻度 + 低変化率 = 最適なキャッシュ候補を探します
// 分析出力の例:
// GET /api/products → 12,000 req/min、30 分ごとに変更 → CACHE (TTL: 5 分)
// GET /api/products/:id → 8,000 req/min、更新時に変更 → CACHE (書き込み時に無効化)
// POST /api/orders → 200 req/min、常に一意 → DO NOT CACHE
// GET /api/user/profile → 3,000 req/min、めったに変更されない → CACHE (TTL: 15 分)
2. cache-aside パターンを実装する (最も一般的)
アプリケーションは最初にキャッシュを確認し、データベースにフォールバックしてから、キャッシュを投入します。
import Redis from "ioredis";
const redis = new Redis({ host: "localhost", port: 6379, maxRetriesPerRequest: 3 });
async function getCached<T>(
key: string,
fetcher: () => Promise<T>,
ttlSeconds: number = 300
): Promise<T> {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const data = await fetcher();
await redis.set(key, JSON.stringify(data), "EX", ttlSeconds);
return data;
}
// ルートハンドラーでの使用例
app.get("/api/products/:id", async (req, res) => {
const product = await getCached(
`product:${req.params.id}`,
() => db.products.findById(req.params.id),
600 // 10 分
);
res.json(product);
});
3. キャッシュスタンピードを防止する
人気のあるキーの有効期限が切れると、数百のリクエストが同時にデータベースにヒットします。
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);
}
}
// 別のプロセスがリフレッシュしています — 待機して再試行します
await new Promise((r) => setTimeout(r, 100));
return getCachedWithLock(key, fetcher, ttlSeconds);
}
4. キャッシュ無効化戦略
関連データに対するパターンベースの無効化:
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");
}
// 製品の更新時: 製品とそれを含むリストを無効化します
async function onProductUpdate(productId: string): Promise<void> {
await redis.del(`product:${productId}`);
await invalidatePattern("products:list:*");
}
例
例 1: API 応答のキャッシュ
プロンプト: "Express API に Redis キャッシュを追加します。/api/catalog エンドポイントは 1 分あたり 15,000 回呼び出されますが、データは管理者が更新を公開したときにのみ変更されます。10ms 未満の応答時間を実現したいです。"
エージェントの出力:
- 接続プーリングと再試行構成を備えた
src/cache/redis-client.tsを追加します /api/catalogを cache-aside パターン、5 分の TTL でラップします- 分散ロックによるスタンピード防止を追加します
src/cache/invalidation.tsを作成します — 管理者公開エンドポイントから呼び出され、カタログキャッシュを破棄します- 応答ヘッダー (
X-Cache: HIT/X-Cache: MISS) を介してキャッシュヒット/ミスメトリクスを追加します
例 2: 多層キャッシュ
プロンプト: "当社の製品 API は 50,000 RPM を処理します。最もホットな 1,000 個のアイテムにインメモリキャッシュを追加し、その他すべてに Redis を追加します。製品は在庫が更新されると変更されます。"
エージェントの出力:
- 最大 1,000 エントリと 60 秒の TTL を持つ LRU キャッシュを使用して
src/cache/memory-lru.tsを追加します - メモリ → Redis → データベースの順にチェックする
src/cache/tiered-cache.tsを追加します - 在庫変更時に両方のキャッシュレイヤーを無効にする
src/events/inventory-handler.tsを作成します - 各レイヤーのヒット率を示す
/admin/cache/statsエンドポイントを追加します
ガイドライン
- Cache-aside がデフォルト — 書き込み時にキャッシュの鮮度が保証されている必要がある場合にのみ、write-through を使用します。
- TTL なしでキャッシュしないでください — "永続的な" データであっても、安全のため、長い TTL (1 時間以上) を設定する必要があります。
- キーの名前空間を使用する —
products:v2:{id}のようにキーにプレフィックスを付けて、キャッシュスキーマをバージョン管理できるようにします。 - ヒット率を監視する — 80% 未満の場合、TTL が短すぎるか、データが速く変化しすぎてキャッシュに適していません。
- 慎重にシリアライズする — JSON.parse/stringify はほとんどの場合に適していますが、大きなペイロードの場合は MessagePack を検討してください。
- Redis のダウンタイムに備える — アプリはクラッシュするのではなく、データベースへの直接クエリに正常にダウングレードする必要があります。
- 適切なキー分離なしに共有キャッシュにユーザー固有のデータをキャッシュすることは避けてください — データ漏洩はセキュリティインシデントです。
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
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";
const redis = new Redis({ host: "localhost", port: 6379, maxRetriesPerRequest: 3 });
async function getCached<T>(
key: string,
fetcher: () => Promise<T>,
ttlSeconds: number = 300
): Promise<T> {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const data = await fetcher();
await redis.set(key, JSON.stringify(data), "EX", ttlSeconds);
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 guaranteed cache freshness on writes.
- Never cache without a TTL — even "permanent" data should have a long TTL (1 hour+) as a safety net.
- Use key namespacing — prefix keys like
products:v2:{id}so you can version your cache schema. - Monitor hit rate — below 80% means your TTL is too short or your data changes too fast for caching.
- Serialize carefully — JSON.parse/stringify is fine for most cases but consider MessagePack for large payloads.
- Plan for Redis downtime — your app should degrade gracefully to direct database queries, not crash.
- Avoid caching user-specific data in shared caches without proper key isolation — data leaks are a security incident.