用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/skeletorflet/opencode-kit --skill caching-strategies命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | caching-strategies |
| description | Caching patterns. HTTP caching, Redis, CDN, browser cache, cache invalidation strategies. |
A cache hit is the fastest request. The fastest database query is no query.
Browser Cache (ms)
↓
CDN / Edge Cache (ms)
↓
Load Balancer Cache (ms)
↓
Application Cache / Redis (ms)
↓
Database Query Cache (ms)
↓
Database (ms–s)
| Header | Purpose | Example |
|---|---|---|
Cache-Control | Directives for browser/CDN | max-age=3600, s-maxage=86400 |
ETag | Conditional request validation | "abc123" |
Last-Modified | Time-based validation | Wed, 21 Oct 2015 07:28:00 GMT |
Vary | Cache key variations | Vary: Accept-Encoding, Cookie |
Cache-Control directives:
├── public → CDN can cache
├── private → Browser only (logged-in content)
├── no-cache → Revalidate before use
├── no-store → Never cache (sensitive data)
├── max-age=N → Browser TTL in seconds
└── s-maxage=N → CDN TTL (overrides max-age for CDN)
| Strategy | Description | Use When |
|---|---|---|
| TTL (Time-to-Live) | Expire after N seconds | Data changes predictably |
| Write-through | Update cache on write | Low write volume, strong consistency |
| Write-behind | Async cache update | High write throughput |
| Cache-aside | App manages cache manually | General purpose |
| Event-driven | Invalidate on domain events | Microservices |
Cache-aside pattern:
1. Read: check cache → hit → return
2. Read: miss → query DB → store in cache → return
3. Write: update DB → DELETE cache key (not update)
# Cache-aside (Python)
import json
from redis import Redis
r = Redis()
TTL = 3600 # 1 hour
def get_user(user_id: str):
key = f"user:{user_id}"
cached = r.get(key)
if cached:
return json.loads(cached)
user = db.query_user(user_id) # DB call
r.setex(key, TTL, json.dumps(user))
return user
def update_user(user_id: str, data: dict):
db.update_user(user_id, data)
r.delete(f"user:{user_id}") # Invalidate
// TypeScript / Node.js
const CACHE_KEY = (id: string) => `user:${id}`;
async function getUser(id: string) {
const cached = await redis.get(CACHE_KEY(id));
if (cached) return JSON.parse(cached);
const user = await db.user.findUnique({ where: { id } });
await redis.setex(CACHE_KEY(id), 3600, JSON.stringify(user));
return user;
}
| Resource | Strategy | max-age |
|---|---|---|
| HTML pages | no-cache or short TTL | 0–300s |
| JS/CSS (hashed names) | Long TTL | 31536000s (1yr) |
| Images (hashed) | Long TTL | 31536000s |
| API responses (public) | Short TTL | 60–300s |
| API responses (private) | private, no-store | — |
Cache-busting with content hashing:
/assets/main.a3f4b2c.js ← filename changes on content change
→ Set Cache-Control: max-age=31536000, immutable
| Pattern | Description |
|---|---|
| Memoization | Cache function result by args |
| Request deduplication | Collapse concurrent identical requests |
| Stale-while-revalidate | Return cached + refresh in background |
| Circuit breaker + cache | Serve cache when downstream fails |
Bad: "users" (too broad, cache pollution)
Good: "users:list:page=1:limit=20:sort=name"
Good: "user:{id}:profile"
Good: "org:{orgId}:feature-flags"
Namespace conventions:
{entity}:{id}:{subresource}
{entity}:list:{filter-hash}
private directive