用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/InugamiDev/ultrathink-oss --skill cache-invalidation命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Unified design foundations — design system architecture, tokens, component specs, visual principles, creative vision, figma integration, plus brand design system loader (66 real brands via DESIGN.md). Absorbs design, design-system, design-systems, design-principles, design-router, creative-vision, figma, design-md.
Render, summarize, and present markdown documents and structured content in multiple output modes
Ultra UI skill - combines Google's DESIGN.md spec (machine-readable design tokens) with the ui-ux-pro-max knowledge base (91 styles, 161 palettes, 73 font pairings, 161 products, 104 UX guidelines, 25 chart types). Generates lint-clean DESIGN.md files, validates token references and WCAG contrast, exports Tailwind/DTCG tokens, and diffs design systems version-over-version.
基于 SOC 职业分类
正在显示 SKILL.md
| name | cache-invalidation |
| description | Cache invalidation — TTL, event-driven, cache-aside, write-through, SWR, tag purge |
| layer | domain |
| category | backend |
| triggers | ["cache invalidation","stale cache","cache busting","cache aside","write through","stale while revalidate","cache purge","tag-based invalidation"] |
| linksTo | ["caching","redis","api-caching"] |
| linkedFrom | ["caching","redis"] |
| riskLevel | medium |
Cache invalidation is the process of removing or updating stale cached data when the underlying source of truth changes. It is famously one of the two hard problems in computer science. The goal is to balance freshness (serving current data) against performance (avoiding unnecessary origin fetches).
Application checks cache first; on miss, reads from DB and populates cache.
async function getProduct(id: string): Promise<Product> {
const cacheKey = `product:${id}`;
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);
const product = await db.query('SELECT * FROM products WHERE id = $1', [id]);
await redis.setex(cacheKey, 3600, JSON.stringify(product)); // 1h TTL
return product;
}
async function updateProduct(id: string, data: Partial<Product>) {
await db.query('UPDATE products SET ... WHERE id = $1', [id]);
await redis.del(`product:${id}`); // invalidate — next read repopulates
}
Write to cache and DB simultaneously. Cache is always fresh but writes are slower.
async function updateProduct(id: string, data: Partial<Product>) {
const updated = await db.query(
'UPDATE products SET ... WHERE id = $1 RETURNING *', [id]
);
await redis.setex(`product:${id}`, 3600, JSON.stringify(updated));
return updated;
}
Decouple invalidation from the write path using events.
// Publisher (in the write service)
await db.query('UPDATE products SET price = $1 WHERE id = $2', [newPrice, id]);
await eventBus.publish('product.updated', { id, fields: ['price'] });
// Subscriber (cache invalidation worker)
eventBus.subscribe('product.updated', async (event) => {
await redis.del(`product:${event.id}`);
await cdn.purgeTag(`product-${event.id}`);
});
// Set tags when caching
// Next.js revalidateTag example
import { revalidateTag } from 'next/cache';
// In a fetch call
fetch('https://api.example.com/products', {
next: { tags: ['products', `category-${categoryId}`] },
});
// On mutation — purge all caches with this tag
export async function updateCategory(id: string) {
await db.updateCategory(id);
revalidateTag(`category-${id}`);
}
Serve stale data immediately while refreshing in the background.
// HTTP header approach
// Cache-Control: public, max-age=60, stale-while-revalidate=300
// Application-level SWR
async function getWithSWR<T>(key: string, fetcher: () => Promise<T>, ttl: number, swrWindow: number) {
const entry = await redis.hgetall(`swr:${key}`);
if (entry.data) {
const age = Date.now() - Number(entry.timestamp);
if (age < ttl * 1000) return JSON.parse(entry.data); // fresh
if (age < (ttl + swrWindow) * 1000) {
// stale but within SWR window — return stale, refresh async
refreshInBackground(key, fetcher, ttl);
return JSON.parse(entry.data);
}
}
// Cache miss or expired beyond SWR window
const fresh = await fetcher();
await redis.hset(`swr:${key}`, { : .(fresh), : .() });
fresh;
}
Vary headers correctly.