Wednesday 26 April 2017

How to implement automatic cache invalidation

Hi,I followed this tutorial and now have a working CRUD ToDo API. That's great, but now I want to implement some caching.If you request all existing ToDo lists, it should try to fetch that list from the cache first. If you update, create or delete a ToDo list it should invalidate the cache that contains the list of all ToDos.Is there anyway of doing that automatically (with some middleware) ?Here's a sample code to retrieve all ToDos:controllers/todos.jslist(req, res) { return Todo .findAll({ include: [{ model: TodoItem, as: 'todoItems', }], }) .then(todos => res.status(200).send(todos)) .catch(error => res.status(400).send(error)); } and here's the update code: update(req, res) { return Todo .findById(req.params.todoId, { include: [{ model: TodoItem, as: 'todoItems', }], }) .then(todo => { if (!todo) { return res.status(404).send({ message: 'Todo Not Found', }); } return todo .update({ title: req.body.title || todo.title, }) .then(() => res.status(200).send(todo)) // Send back the updated todo. .catch((error) => res.status(400).send(error)); }) .catch((error) => res.status(400).send(error)); }, How can I implement some sort of caching in that code. Is there a way to automatically delete all cache from the models that are updated (and any model that is included)

Submitted April 26, 2017 at 07:51AM by sofuj

No comments:

Post a Comment