In this post, I would like to show you, how we can use Redis to Cache APIs response to avoid multiple hits to the API. Same technique you can use to avoid multiple database calls ..etc
We gonna create the express.js application to demonstrate Redis cache example.
let’s create a folder called
After creating, change the directory to
Now initialize the project with
npm init command
--yes tells npm to use the default options while generating the package.json so you are not asked any questions
After running the command above, you should have a
package.json file in the
Now install the Express, Redis, and node-fetch npm modules with below command
Create a file
server.js with following code in the
All set now start you node server with
node server.js command from your terminal, but before doing that ensure you have Redis server installed on your computer and update the Redis port if it is not running at default port 6379.
Now head over to the browser with
http://localhost:3000, at first visit in the response object you should see source key value as API and if you refresh the page you should see source key value as a cache.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.