Global configuration for the cache instance.
const cache = memorize({ ttl: 30_000 });
app.get('/users', cache(), (req, res) => {
res.json({ data: users });
});
const cache = memorize({ ttl: 60_000 });
app.use(cache()); // caches all GET routes
const cache = memorize({ ttl: 30_000 });
app.post('/users', (req, res) => {
users.push(req.body);
cache.delete('/users');
res.status(201).json(req.body);
});
cache.on(MemorizeEventType.Set, (e) => console.log('stored', e.key));
cache.on(MemorizeEventType.Delete, (e) => console.log('deleted', e.key));
cache.on(MemorizeEventType.Expire, (e) => console.log('expired', e.key));
cache.on(MemorizeEventType.Empty, () => console.log('cache is empty'));
Creates an in-memory cache for an Express application.
Returns a Memorize instance that can be used as per-route middleware, a global
app.use()middleware, or a cache management API — all sharing the same underlying store.Only
GETrequests are cached. Responses are cached only when the HTTP status code is in the2xxrange. The cache key isreq.originalUrl, which includes the query string.