Agent Skills
› cosmicstack-labs/mercury-agent-skills
› caching-strategies
caching-strategies
GitHub提供CDN、Redis及内存缓存策略,涵盖Cache-Aside等读写模式、失效机制及Redis最佳实践,旨在优化性能并降低负载。
Trigger Scenarios
查询缓存架构设计
选择缓存写入策略
解决缓存失效问题
Redis配置与优化建议
Install
npx skills add cosmicstack-labs/mercury-agent-skills --skill caching-strategies -g -y
SKILL.md
Frontmatter
{
"name": "caching-strategies",
"metadata": {
"tags": [
"caching",
"performance",
"redis",
"cdn",
"distributed-systems"
],
"author": "cosmicstack-labs",
"version": "1.0.0",
"category": "backend"
},
"description": "CDN, Redis, in-memory cache, cache invalidation, and distributed caching patterns"
}
Caching Strategies
Cache effectively to improve performance and reduce load.
Cache Layers
| Layer | Latency | Storage | Examples |
|---|---|---|---|
| Browser | Local | Small | localStorage, HTTP cache |
| CDN | Regional | Large | CloudFront, CloudFlare, Fastly |
| Application | In-process | Small | L1 cache (heap) |
| Distributed | Network | Large | Redis, Memcached |
| Database | Same host | Very large | Buffer pool, query cache |
Caching Patterns
Cache-Aside (Lazy Loading)
1. Check cache → miss
2. Query database
3. Store in cache
4. Return
Best for: Read-heavy, general purpose.
Write-Through
1. Write to cache
2. Cache writes to DB (sync)
3. Return
Best for: Consistency critical, write-heavy.
Write-Behind
1. Write to cache
2. Return immediately
3. Cache writes to DB (async)
Best for: High throughput writes, eventual consistency OK.
Refresh-Ahead
- Cache proactively refreshes before expiry
- Reduces latency spikes on cache miss
- Requires predictive logic or scheduled refresh
Cache Invalidation
Two hard things: naming, cache invalidation, off-by-one errors.
| Strategy | How | Risk |
|---|---|---|
| TTL | Expire after X seconds | Stale data during TTL |
| Event-based | Invalidate on data change | Missed events |
| Version keys | user_42_v3 |
Unused old keys accumulate |
| Write-through | Update cache on write | Write amplification |
Redis Patterns
- Use
EXPIREon every SET - Use
SCANnotKEYSin production - Distributing: Redis Cluster for sharding
- Backup: RDB (snapshot) + AOF (append-only log)
- Monitor: hit rate, memory, evictions, latency
Version History
- 38e2523 Current 2026-07-05 19:36


