lru-send
Version:
LRU-based caching middleware for Node.js
24 lines (21 loc) • 667 B
JavaScript
import LRU from 'quick-lru';
const lruSend = (lruInstance) => {
const cache = lruInstance || new LRU({ maxSize: 1000, maxAge: 1000 * 60 });
return (req, res, next) => {
if (req.method === 'GET') {
const key = `${req.url}.${req.headers['accepts']}.${req.headers['accept-encoding']}`;
const value = cache.get(key);
if (value) {
res.send(value);
return;
}
const _send = res.send;
res.send = (body) => {
cache.set(key, body);
return _send(body);
};
}
next();
};
};
export { lruSend };