Intermediate · 8 lessons
Caching
Caching stores copies of data closer to users or in faster storage layers, avoiding expensive recomputation or network requests. Effective caching can reduce server load by 80%+ and cut page load times dramatically.
Key Concepts
Key Takeaways
- ✓ Cache at every layer: browser → CDN → page → object → database.
- ✓ Set proper Cache-Control headers for static assets (max-age=31536000).
- ✓ Redis object cache eliminates repeated database queries.
- ✓ Always have a cache purge strategy when deploying content updates.
Lessons in This Topic
Caching Layers Explained
Browser, CDN, page, object, and opcode cache.
Each layer serves cached data faster than regenerating it. Browser cache: client-side, instant for repeat visits. CDN: edge servers near users. Page cache: pre-rendered HTML. Object cache: DB query results in Redis. OPcache: compiled PHP bytecode in memory.
Browser & HTTP Caching
Cache-Control, ETags, and Expires headers.
Cache-Control: max-age=3600 caches for 1 hour. immutable tells browser never revalidate. ETags enable conditional requests (304 Not Modified). Set long cache for versioned assets (/app.v2.js). Short or no cache for dynamic HTML pages.
WordPress Caching Plugins
LiteSpeed, WP Rocket, W3 Total Cache.
Page cache plugins generate static HTML for each URL. Object cache plugins connect to Redis/Memcached. LiteSpeed Cache is free and powerful on LiteSpeed servers. WP Rocket is premium but easiest to configure. Always exclude logged-in user pages and cart/checkout from cache.
Redis Object Caching
Store database queries in memory.
Install Redis server. Use Redis Object Cache plugin for WordPress. Transients and wp_options queries served from RAM. Reduces MySQL load by 50-80%. Set maxmemory policy to allkeys-lru. Monitor with redis-cli INFO.
CDN Caching & Purging
Edge cache management for global delivery.
CDN caches static assets at 200+ PoPs worldwide. Set cache rules by file type and path. Purge entire cache or specific URLs after deployments. Use cache tags for granular invalidation. Stale-while-revalidate serves old content while fetching fresh copy.
