Web caching includes browser cache (HTTP headers like Cache-Control), CDN caching for static assets, application cache (localStorage, sessionStorage, IndexedDB), and service worker caching for offline support. Each serves different purposes.
Types of Caching:
Browser Cache (HTTP)
CDN Cache
Application Cache
Service Worker Cache
// Express.js cache headers
app.use('/static', express.static('public', {
maxAge: '1y', // Cache static assets for 1 year
immutable: true,
}));
// Different caching for different content
app.get('/api/data', (req, res) => {
// No caching for API responses
res.set('Cache-Control', 'no-store');
res.json(data);
});
app.get('/api/config', (req, res) => {
// Cache for 1 hour, allow stale while revalidating
res.set('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
res.json(config);
});
// Common Cache-Control values:
// - no-store: Never cache
// - no-cache: Cache but revalidate
// - public, max-age=31536000: Cache for 1 year
// - private, max-age=3600: User-specific, 1 hour