If you are using ExpressJS as your web framework, you can add middle-ware functions to easily handle the caching mechanism with elegant code.
You may use setex with an expiry value and add a middleware as give below for the request.
app.get('/repos', cache, getNumberOfRepos);
You may use native redis package to initialize connection
An Example middleware function is given below
function cache(req, res, next) {
const org = req.query.org;
client.get(org, function (err, data) {
if (err) throw err;
if (data != null) {
res.send(respond(org, data));
} else {
next();
}
});
}
Try this out: community.risingstack.com/redis-node-js-introduct…