Command Palette

Search for a command to run...

YepAPI
Free · All Tools

Caching Strategy Agent Skill

Redis caching, CDN cache headers, stale-while-revalidate, cache invalidation, React Query.

cacherediscdnperformance

The Skill

Full content, every format. Copy it, download it, or install with one command.

SKILL.md
---
description: Redis caching, CDN cache headers, stale-while-revalidate, cache invalidation, React Query.
homepage: https://yepapi.com/skills/caching-strategy
metadata:
  tags: [cache, redis, cdn, performance]
---

# Caching Strategy

## Rules

- Cache at every layer: browser (Cache-Control), CDN (edge), application (Redis), database (query cache)
- Use `stale-while-revalidate` for content that can be slightly stale — serve fast, refresh in background
- Cache-Control headers: `public, max-age=31536000, immutable` for hashed static assets; `public, s-maxage=60, stale-while-revalidate=300` for dynamic pages
- Redis caching: serialize to JSON, set TTL on every key, use namespaced keys (`cache:users:123`)
- React Query / TanStack Query: set `staleTime` (how long data is fresh) and `gcTime` (how long to keep in memory)
- Invalidate on write — when data changes, delete or update the cache entry immediately
- Use cache-aside pattern: check cache first, fetch from source on miss, write to cache
- For Next.js: leverage ISR (`revalidate`), `unstable_cache`, and fetch cache for server components
- Tag-based invalidation: tag cached entries by entity type, invalidate all entries for a tag on mutation

## Patterns

```ts
// Cache-aside with 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;
}
```

## Avoid

- Caching without TTL — stale data will persist indefinitely
- Caching user-specific data in shared CDN cache — use `private` or `Vary` headers
- Cache stampede: use locking or `stale-while-revalidate` to prevent thundering herd
- Caching error responses — only cache successful results

Install

Why Use the Caching Strategy Skill?

Without this skill, your AI guesses at caching strategy patterns. It might hallucinate deprecated APIs, use outdated conventions, or miss best practices entirely. With it, your AI follows a proven ruleset — every suggestion aligns with current standards.

Drop this skill into your project and your AI instantly knows the rules. Better code suggestions, fewer errors, faster shipping.

Try These Prompts

These prompts work better with the Caching Strategy skill installed. Your AI knows the context and writes code that fits.

"Implement Redis caching with stale-while-revalidate and automatic cache invalidation"

"Set up a multi-layer caching strategy with CDN, Redis, and React Query"

"Add cache headers to API routes with proper ETags and conditional request handling"

Caching Strategy skill — FAQ

It provides Redis caching patterns, CDN cache headers, stale-while-revalidate, and cache invalidation strategies. Your AI implements multi-layer caching that actually works without stale data issues.

Run `npx skills add YepAPI/skills --skill caching-strategy` in your project root. This copies the skill file into your repo where your AI coding tool can read it automatically.

The skill covers time-based expiry, event-based invalidation on writes, and stale-while-revalidate for balancing freshness with performance. It includes Redis patterns and CDN cache header strategies.

Want more skills?

Browse all 110 free skills for builders.

See All Skills