This project was based in the ITNEXT Article which explain the usage of Redis as a cache database, but there are some improvements in the structure of the code to make the example more clear and easy to understand.
Run the project
This extract of code ensure that the result of the request will be stored in Redis.
const todos = await axios.get('/todos');
RedisClient.setex(req.route.path, 3600, JSON.stringify(todos.data));
The next piece of code shows how to create a middleware to verify if there are some data stored in Redis or not.
export const checkCache = (req: Request, res: Response, next: NextFunction) => {
RedisClient.get(req.route.path, (err, data) => {
if (err) {
console.log(err);
return res.status(500).send(err);
}
if (data != null) {
const todos = JSON.parse(data);
return res.json(todos);
} else {
next();
}
})
}
Following this simple example you will be able to implements a more complex case of caching data with Redis.