trinhhaox/ag-kit/caching-strategies

Claude Code and agent skill by trinhhaox/ag-kit

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Multi-layer caching patterns - Browser, CDN, Redis, app-level, DB cache. Invalidation strategies, ISR, SWR, thundering herd prevention.

Install

npx vskill@latest install trinhhaox/ag-kit/caching-strategies

SKILL.md

Caching Strategies

End-to-end caching patterns from browser to database.

5-Tier Caching Pyramid

Browser Cache (fastest, per-user)
→ CDN / Edge Cache (regional, shared)
→ Reverse Proxy (Nginx, Varnish)
→ Application Cache (Redis, in-memory)
→ Database Cache (query cache, materialized views)

Primary Strategies

Cache-Aside (Lazy Loading)
typescript
async function getData(key: string) {
let data = await redis.get(key);
if (!data) {
data = await db.query(key);
await redis.set(key, data, { EX: 3600 });
}
return data;
}

Use when: Read-heavy, tolerance for occasional stale data.

Write-Through
typescript
async function saveData(key: string, value: any) {
await db.save(key, value);
await redis.set(key, value, { EX: 3600 });
}

Use when: Consistency matters, write frequency is moderate.

Write-Behind (Write-Back)
  • Write to cache immediately, async flush to DB
  • Risk: Data loss if cache crashes before flush
  • Use when: High write throughput, eventual consistency OK
Read-Through
  • Cache fetches from DB automatically on miss
  • Simpler code, cache library handles population
Stale-While-Revalidate (SWR)
typescript
// Return stale data immediately, refresh in background
Cache-Control: max-age=60, stale-while-revalidate=300

HTTP Cache Headers

DirectivePurpose
Cache-Control: public, max-age=31536000Static assets (immutable)
Cache-Control: private, no-cachePer-user data, always revalidate
Cache-Control: s-maxage=3600, stale-while-revalidate=86400CDN cache with SWR
ETag: "abc123"Conditional requests (304 Not Modified)
Vary: Accept-Encoding, AuthorizationCache variants

Redis Patterns

Key Naming Convention
{service}:{entity}:{id}:{field}
# e.g., app:user:123:profile
TTL Guidelines
Data TypeTTLReason
User sessions30min-24hSecurity
API responses5min-1hFreshness
Static config24h-7dRarely changes
Computed results1h-24hCost of recompute
Invalidation Patterns

Event-Driven:

typescript
// On data change, publish invalidation event
await redis.del(`app:user:${userId}:profile`);
await redis.publish('cache:invalidate', JSON.stringify({ key, reason }));

Tag-Based:

typescript
// Tag cache entries, invalidate by tag
await redis.sadd('tag:user:123', 'app:user:123:profile', 'app:user:123:orders');
// Invalidate all entries for user 123
const keys = await redis.smembers('tag:user:123');
await redis.del(...keys);

Versioned Keys:

typescript
const version = await redis.incr('version:products');
const key = `products:v${version}:list`;

Next.js Specific

ISR (Incremental Static Regeneration)
typescript
// app/products/[id]/page.tsx
export const revalidate = 3600; // Revalidate every hour
// On-demand revalidation
// app/api/revalidate/route.ts
import { revalidatePath, revalidateTag } from 'next/cache';
export async function POST(req: Request) {
revalidateTag('products');
return Response.json({ revalidated: true });
}
fetch() Caching
typescript
// Cache indefinitely (default in Next.js)
fetch(url, { cache: 'force-cache' });
// Revalidate every 60 seconds
fetch(url, { next: { revalidate: 60 } });
// Never cache
fetch(url, { cache: 'no-store' });
// Tag-based
fetch(url, { next: { tags: ['products'] } });

Common Pitfalls

PitfallSolution
Cache stampedeMutex/lock on cache miss, only one request populates
Thundering herdStagger TTLs with jitter: TTL + random(0, TTL*0.1)
DogpilingBackground refresh before expiry
Key explosionLimit key cardinality, use hash structures
Stale dataEvent-driven invalidation, short TTLs for mutable data
Cold startCache warming on deploy

Monitoring Targets

  • Hit rate > 90%
  • P99 latency < 5ms (Redis)
  • Eviction rate stable (not climbing)

Security

  • Security scanPassed, 90 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

7-day trend score
0

Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/trinhhaox/ag-kit/caching-strategies/badge)](https://verified-skill.com/skills/trinhhaox/ag-kit/caching-strategies)

Something wrong with this skill or its page? Tell us.