Communitygithub.com

YepAPI/skills

Redis 缓存、CDN 缓存头、stale-while-revalidate、缓存失效策略、React Query。

skills 是什么?

该技能专注于跨多个层级实施全面缓存策略,包括浏览器(Cache-Control)、CDN(边缘节点)、应用层(Redis)和数据库(查询缓存)。它详细说明了如何为不同类型的资源正确设置缓存头,例如对哈希后的静态资源使用 `public, max-age=31536000, immutable`,对动态页面使用 `public, s-maxage=60, stale-while-revalidate=300`。技能涵盖 Redis 缓存的最佳实践,包括 JSON 序列化、设置 TTL 和使用命名空间键,以及 React Query / TanStack Query 的配置(`staleTime` 和 `gcTime`)。此外,还介绍了缓存失效模式(写入时失效)、缓存旁路模式、Next.js 的 ISR 和 `unstable_cache`,以及基于标签的无效化策略。此技能适用于使用 Node.js、Next.js、Redis 和 React Query 等工具的开发者,帮助他们优化应用性能和响应速度。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/caching-strategy

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

缓存策略

规则

  • 在每一层进行缓存:浏览器(Cache-Control)、CDN(边缘)、应用层(Redis)、数据库(查询缓存)
  • 对可以稍微过期的内容使用 stale-while-revalidate —— 快速响应,后台刷新
  • 缓存头:对哈希后的静态资源使用 public, max-age=31536000, immutable;对动态页面使用 public, s-maxage=60, stale-while-revalidate=300
  • Redis 缓存:序列化为 JSON,为每个键设置 TTL,使用命名空间键(cache:users:123)
  • React Query / TanStack Query:设置 staleTime(数据的有效期)和 gcTime(数据在内存中的保留时间)
  • 写入时失效 —— 当数据发生变化时,立即删除或更新缓存条目
  • 使用缓存旁路模式:先检查缓存,未命中时从源获取,再写入缓存
  • 对于 Next.js:利用 ISR(revalidate)、unstable_cache 和获取缓存的服务器组件
  • 基于标签的无效化:按实体类型标记缓存条目,在变更时使该标签的所有条目失效

模式

// 缓存旁路模式与 Redis
async function getCachedUser(id: string) {
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);
  const user = await db.user.findUnique({ where: { id } });
  await redis.set(`user:${id}`, JSON.stringify(user), "EX", 3600);
  return user;
}

避免事项

  • 不设置 TTL 的缓存 —— 过期数据将无限期存在
  • 在共享 CDN 缓存中缓存用户特定数据 —— 应使用 private 或 Vary 头
  • 缓存雪崩:使用锁或 stale-while-revalidate 防止惊群效应
  • 缓存错误响应 —— 只缓存成功的结果

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能