UNPKG

lru-send

Version:

LRU-based caching middleware for Node.js

26 lines (23 loc) 677 B
import * as Redis from 'ioredis'; const lruSend = (redisInstance) => { const cache = redisInstance || new Redis(); return async (req, res, next) => { const key = `${req.url}.${req.headers['accepts']}.${req.headers['accept-encoding']}`; const value = await cache.get(key); if (value) { res.send(value); return; } const _send = res.send; res.send = (body) => { cache.set(key, body).then((r) => { if (r === 'OK') { return _send(body); } }); return res; }; next(); }; }; export { lruSend };