Redis in NodeJS With Example
Caching is very powerful technique while fetching the data. Redis provide very easy way to implement the caching technique in Nodejs. Redis (caching) makes your search faster and improve your performance. In now days, Redis become a very common tool to use for caching. In this article we will see how we can implement the Redis in our Nodejs applications.What is Redis?
Redis is an Open Source memory optimization tool that can be used as a database for caching. It store data in key-value pair in the memory. It is also used to store multiple data structure and different datatypes. It make query faster and provide results in least amount of time. If client fetches any data then system will first look into redis if that data is there then it will fetch from redish and then return to the client otherwise it will fetch the data from given source and then store this data into Redis for the next fetch. And the second time fecth will be done from Redis without any DB operations.Implement Redis in Nodejs
In this article, we'll see how we ca implement the Redis in nodejs application. We will learn from scratch. We will install the Redis and create a node application to implement the caching using Redis, later we will fetch the data from Redis and see how it's working.Install the Redis:
I hope you have installed the Nodejs if not then install Nodejs first. After installing nodejs you need to install Redis using following commands:
or
I prefer brew, if your are using MacBook.
Redis in NodeJS With Example
Start the Redis Server:
You you need to start the Redis server by following command.
Redis in NodeJS With Example
Implement Redis in Node Server:
Now It's time to implement the Redis into out Node Server. Before implementing this let's have a look of directory structure, so that we'll have better understanding abut the implementations.So, here are the directory structure:
Let's write the code and understand the codes fo above files.
redisConstant.ts
The above codes are configurtion to connect the Redis to local env or development env.
redisConnection.ts
Let's understand the above function getOrSetCache().
The method getOrSetCache() will be called to get the data from Redis or set the Data in Redis, If data is not present in the Redis then this will call a callback function which is coming from calling function, and this callback function get the data from DB and store into the Redis. key - This is the key that will be responsible to get the or set a particular data. fetchDbData - Callback function. cacheDurationInSeconds - Cache memory duration, the data will be stored into the cache till the given cacheDurationInSeconds time. after that it will be expired and deleted from the cache.
redisController.ts
In the redisController.ts, we'll write our logic to fetch the data from Redis (cache) or set the data into Redis (cache).
In the above code, we want to get the current App Version by using key current_app_version, so if the currentAppVersionis available in the cache then getOrSetCache() will fetch from cache and return it, otherwise getOrSetCache() will call the below callback function which is passing as an argument:
and get the current version from the DB and store this data into cache againes key current_app_version, and next time when someone want to fetch the data by key 'current_app_version' then it will fetch it from the cache if the cache data is not yet expired.
Let's have a full codes with complete example here
Conclusion:
In this article, we have seen about Redis, What is Redis, Implement Redis in Nodejs and Installation the Redis. We have implemented Redis and create our cache in NodeJS, We have fetch the data from Redis and stored the data in Redis with expiry.Strongly Recommended Books For You:
Reading books opens the door to allow in more lights, If we want to know a man of rare intellect, try to know what books he/she reads.For India:
For Other Countries (United States, United Kingdom, Spain, Italy, Germany, France, Canada, etc)
Thank you
I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go.
If you’re interested in Node.js or JavaScript this link will help you a lot.
If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also.
You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please
subscribe us, You'll get the monthly newsletter having all the published
article of the last month.