Wednesday, 23 September 2020

How to listen for when response is sent to the client in express? (for caching routes with redis)

I want to create a response caching (w/ redis) middleware, where I cache the response of some routes for X amount of time and if the cache exists then send it in the response, otherwise pull the data off of the API or the DB and cache the response and then send the response...I wanna do it in a very simple way where all I need to do is just add the middleware to a certain routes and it would just work (without having to manually add the caching code before sending the response in each of the routes)Therefore I'm looking for a way to listen to routes finishing their job and sending the response to the user (like some libraries like "morgan" or other logger libraries do) and get the response then cache it in redis...BTW I'm aware that there might be some middlewares out there that might do that job, but I have a bit more complex situation in mind that would be solved only by doing the redis caching manually like I described above...Thanks in advance :)

Submitted September 23, 2020 at 06:32PM by s_trader

No comments:

Post a Comment