Skip to content

Instantly share code, notes, and snippets.

@maapteh
Last active December 11, 2019 04:11
Show Gist options
  • Select an option

  • Save maapteh/895b0a50a1d5f8bca9089fe80b865127 to your computer and use it in GitHub Desktop.

Select an option

Save maapteh/895b0a50a1d5f8bca9089fe80b865127 to your computer and use it in GitHub Desktop.
caching
function (duration) {
// cutted code
return (req, res, next) => {
// cutted code
if (res.locals.ratelimited) {
if (cacheContent) {
res.setHeader(cacheKeyHeader, 'HIT');
res.status(200).send(cacheContent);
return;
} else {
res.status(500).send(errorPage);
return;
}
}
if (!isMember && cacheContent) {
res.setHeader(cacheKeyHeader, 'HIT');
res.status(200).send(cacheContent);
return;
}
if (!cacheContent) {
res.locals.sendResponse = res.send;
res.setHeader(cacheKeyHeader, 'HIT');
res.send = (body) => {
if (res.statusCode === 200) {
memCache.put(key, body, duration * 1000);
}
res.locals.sendResponse(body);
}
}
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment