Epoch Invalidation β
Invalidating cached data is notoriously difficult. Most libraries struggle with performance when deleting many keys at once.
The O(1) Solution β
@express-route-cache uses Epoch Versioning. Every route pattern (e.g., /users) has a version number (an "epoch") stored in the cache.
- Storage: When caching
/users/123, we include the current epoch of/usersin the key. - Invalidation: To invalidate all
/users/*routes, we simply increment the epoch of/users. - Result: All existing cache keys for
/usersinstantly become "ghosts"βthey still exist in the database but will never be queried again because the application is now looking for a higher epoch version.
Usage β
Manual Invalidation β
ts
// Increment epoch for /users
await cache.invalidateRoute('/users');Middleware Invalidation β
ts
app.post('/users', cache.invalidate('/users'), createUser);Auto Invalidation β
Set autoInvalidate: true to automatically increment epochs for matching patterns when a successful POST, PUT, or DELETE request occurs on a route.
ts
const cache = createCache({ autoInvalidate: true });