mirror of
https://github.com/duthaho/claudekit.git
synced 2026-06-10 12:14:57 +03:00
2.9 KiB
2.9 KiB
name, description
| name | description |
|---|---|
| caching | Use when implementing memoization, HTTP cache headers, Redis caching, CDN configuration, or in-memory caches. Also activate whenever code deals with Cache-Control headers, ETags, functools.lru_cache, React useMemo, TanStack Query cache, or any caching strategy. Applies to cache invalidation, TTL policies, and cache-aside patterns. |
Caching
When to Use
- Memoizing expensive function calls (lru_cache, useMemo, node-cache)
- Setting HTTP cache headers (Cache-Control, ETag, Last-Modified)
- Implementing Redis cache-aside pattern for database query results
- Configuring CDN caching for static assets and API responses
- Building multi-layer caches (in-memory + Redis + CDN)
- Implementing cache invalidation strategies
When NOT to Use
- Data that changes on every request (real-time prices, live feeds)
- Security-sensitive responses that must never be cached (auth tokens, personal data)
- Development environments where stale data causes confusion
Quick Reference
| Topic | Reference | Key content |
|---|---|---|
| All caching patterns | references/patterns.md |
Memoization, HTTP headers, ETags, Redis, CDN, multi-layer, invalidation |
| Decision tree | references/caching-decision-tree.md |
When to use which caching strategy |
Best Practices
- Cache at the right layer. In-memory for hot paths (<1ms), Redis for shared state (<5ms), CDN for static/semi-static content.
- Always set TTLs. Every cache entry must expire. Unbounded caches grow until they crash.
- Use cache-aside (lazy loading) by default. Read from cache, miss goes to DB, write result to cache. Simplest and most predictable pattern.
- Invalidate on write. When data changes, delete the cache key immediately. Don't wait for TTL expiry.
- Use ETag-based validation for HTTP caching. Cheaper than full responses and guarantees freshness.
- Prevent cache stampede. When a popular key expires, use distributed locks or stale-while-revalidate to prevent all requests from hitting the DB simultaneously.
- Monitor cache hit rates. A cache with <80% hit rate may not be worth the complexity. Measure before optimizing.
Common Pitfalls
- Caching without TTL — memory grows unboundedly until OOM.
- Cache invalidation bugs — stale data served after writes. Always invalidate on mutation.
- Caching user-specific data with shared keys — one user sees another's data.
- Over-caching in development — confusing stale responses with bugs.
- Ignoring serialization costs — caching large objects in Redis costs more in ser/deser than the DB query saved.
- Not handling cache failures gracefully — if Redis is down, fall through to DB, don't crash.
Related Skills
databases— Redis patterns and database query optimizationbackend-frameworks— Framework-specific cache middlewarefrontend— React useMemo, TanStack Query cache