# Caching Strategies

> Use when implementing caching strategies for applications.

- Skill: `loopyluci/caching-strategies` (Agent Skill)
- Install (CLI): `npx skillmds@latest add loopyluci/caching-strategies`
- Raw SKILL.md: https://api.skillmd.com/api/skills/loopyluci/caching-strategies/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: LoopyLuci (https://skillmd.com/u/loopyluci)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/loopyluci/caching-strategies

---


# Caching Strategies

Implementing caching strategies for web applications, APIs, and distributed systems — from in-memory through distributed cache, CDN, and cache invalidation patterns.

## When to Use

- Reducing database load for frequently accessed data
- Improving API response times
- Implementing distributed caching for scalability
- Designing cache invalidation strategies
- Choosing between local, distributed, and CDN caching

## Caching Patterns

```python
CACHE_PATTERNS = {
    'cache_aside': 'App checks cache first, loads from DB on miss, populates cache',
    'read_through': 'Cache loads from DB automatically on miss',
    'write_through': 'Data written to cache and DB simultaneously',
    'write_behind': 'Data written to cache immediately, DB asynchronously',
    'write_around': 'Data written to DB directly, cache invalidated',
    'refresh_ahead': 'Cache proactively refreshes before expiration',
}

class CacheAside:
    """Cache-Aside pattern implementation."""
    def __init__(self, cache, db):
        self.cache = cache
        self.db = db
    
    def get(self, key: str) -> any:
        result = self.cache.get(key)
        if result is not None:
            return result
        result = self.db.query(key)
        self.cache.set(key, result, ttl=300)
        return result
```

## Common Pitfalls

1. **Stale data** — cache invalidation is one of the hardest problems in CS
2. **Cache stampede** — many requests miss cache simultaneously, overloading DB
3. **Thundering herd** — multiple requests regenerate cache at same time; use locking
4. **Memory overuse** — caching too much data evicts useful data; set TTLs wisely
5. **Distributed cache consistency** — nodes can have different cached versions

## Verification Checklist

- [ ] Cache hit ratio > 80% for hot data
- [ ] TTLs set appropriately for data freshness needs
- [ ] Cache stampede protection (mutex/lock on miss)
- [ ] Monitoring on cache hit/miss ratios
- [ ] Invalidation strategy defined for data updates

