Production caching infrastructure for Express โ routes, services, and beyond.
Cache any Express route or async function with SWR, O(1) invalidation, and stampede protection. Redis, Memcached & in-memory adapters. TypeScript-first.
Epoch Invalidation
Instant nested key updates without scanning Redis keysDB Query Reduction
Saves databases from melting under peak concurrent loadAvg Cache-Hit Latency
SWR serving from Redis on loopback โ benchmark in READMETypeScript-First API
Fully type-safe schemas, options, and event listenersExplore Caching Mechanics
Click the tabs to toggle code patterns, and run the simulator to see the background orchestration logs in action.
import express from 'express';
import { createCache, createRedisAdapter } from '@express-route-cache/core';
const app = express();
const cache = createCache({
adapter: createRedisAdapter({ host: 'localhost' }),
staleTime: 60, // Fresh for 60s
gcTime: 300, // Keep stale for 5m
swr: true // Background revalidation
});
// Cache this route globally
app.get('/api/users/:id', cache.route(), (req, res) => {
res.json({ id: req.params.id, name: 'Alex' });
});Deflect Cache Stampedes
When a high-traffic cache key expires, the "thundering herd" hits your database simultaneously, causing load spikes or server crashes.
Our two-tier lock coordinates requests process-wide (via memory coalescing) and cluster-wide (via Redis/Memcached SETNX). Follower requests poll the cache silently while one leader generates the data.
Anatomy of a Versioned Key
How we compose deterministic, instant-invalidating Redis keys. Click the segments to inspect how our versioning engine works.
Epoch Version counters (O(1) invalidation)
Appends version counters for the route and its ancestor path nodes. By incrementing 'v:api/users', any cached requests referencing it instantly mismatch on key lookup. This invalidates millions of sub-paths (like /api/users/123/profile) in O(1) time without scanning Redis keys.
How We Compare
We built express-route-cache to solve production infrastructure challenges.
| Feature | @express-route-cache | apicache | express-cache-controller | node-cache-manager |
|---|---|---|---|---|
| Invalidation | O(1) Epoch Increment | O(N) SCAN (blocks Redis) | Browser only, no server cache | Manual key deletion |
| Stampede Guard | Memory + Redis coalescing | None โ DB melts | None | None |
| SWR Background Refresh | Built-in | None (blocking misses) | None | None |
| Non-Route Caching | cache.fetch() API | No (middleware only) | No | Yes (manual boilerplate) |
| TypeScript-first | Yes โ 100% | No | No | Partial |
| Visual Dashboard | Cache Studio (standalone) | None | None | None |
Invalidation
Stampede Guard
SWR Background Refresh
Non-Route Caching
Visual Dashboard
Comparison based on publicly documented features. Full comparison โ