const cache = () => { return async(req, res, next) => { const parseUrl = queryString.parseUrl(req.url); const stringifyQuery = queryString.stringify(parseUrl.query); const cacheKey = `__req__${parseUrl.url}?${stringifyQuery}`; let cachedResponse = null; try { cachedResponse = await redisClient.get(cacheKey); } catch (err) { console.error('Error retrieving response from cache', err); } if (cachedResponse) { res.set('Cache-Control', `private, max-age=${config.get('cache.maxAge')}`); res.json(JSON.parse(cachedResponse)); return; } else { res.sendResponse = res.send; res.send = async(body) => { await redisClient.set(cacheKey, body); res.sendResponse(body); } next(); } }; }