How to implement memcached in nodejs application
Today, I am going to explain how we can install memcached in linux system and use it in our nodejs application.
As we know that performance matters for any mobile or web application and Caching is one of the easiest ways to increase any application performance. Lets try to understand the basics of memcached and how to use it in any nodejs application.
What is Memcached?
Memcached is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached is free and open-source software, licensed under the Revised BSD licence. Memcached runs on Unix-like operating systems (at least LINUX and OS X) and on Microsoft windows.
We can store data to memcached server in key pair format. So whenever any request come from the app can be matched with memcached server without any query from mysql/Nosql server. This increases the performance of the application.
Now lets move to intigration part with nodejs application. Here I am using ubuntu machine, So with below command we can install memcahced in our ubuntu machine
Once the above command run successfully, Check whether it is properly installed or not with below command.
You will see something like below:
If you are gettting anything different from above then correct the issue within your system before proceeding ahead.
Now we will create package.json file like below
now move to to project folder and run npm install
After installing the required package. Let's have a look inside index.js file.
/* include required package for nodejs app */
In the above file, At the top we have included required package for nodejs app with memcached. Below that we have created object for memcached and then connection with memcached server. At the middle of the file we have created an object named user to store over memcached server. Now we have three methods first memcached.set(), for saving the user data to memcached.
Where memcached.set(key, value, lifetime, callback)
key: String the name of the key
value: Mixed Either a buffer, JSON, number or string that you want to store.
lifetime: Number, how long the data needs to be stored measured in seconds
callback: Function the callback
Second, memcached.get() to get the saved userdata from the memcached server. Third memcached.del(), to delete the saved data of user whenever required.
We can also get data saved at multiple keys, For that we will use below method:
For more detail visit official website : Click here to redirect After running the app by typing the node index.js, we will get below output over terminal:
That’s all for now. Thank you for reading and I hope this post will be very helpful for implementation of memcached in nodejs application.
Let me know your thoughts over the email email@example.com. I would love to hear them and If you like this article, share with your friends.
You can download complete code from here. Download Code