In this tutorial i am going to show you how you can install memcached in your ubuntu machine and make connection with memcached server from your nodejs application.

Memcached ?

Memcached (Mem-Cache-D) 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 load on your database server.

You can store your mysql data in memcached server in key pair format, So that if same mysql request comes again then you can directly fetch data form memcached server without sending request to your mysql server so with this you can reduce load on your mysql server and make your application fast.




Ok now let’s start the tutorial.

With below command you can install memcahced in your ubuntu machine.

sudo apt-get install memcached

Use this command to check memeched has beed installed in your machine or not

 telnet 127.0.0.1 11211

nodejs-memcached
I assumed that you have already installed and configure nodejs in your machine if not follow this article.
http://www.iamrohit.in/node-js-installation-and-configuration-for-beginners/

Create package.json file in your project directory to install the dependencies

package.json

{
  "name": "NodeJs-Memcached",
  "version": "0.0.1",
  "description": "Simple Nodejs with mamcached tutorial",
  "dependencies": {
    "express": "^4.13.3",
    "memcached": "^2.2.0"
  }
}



After that goto your project directory from terminal and run

cd nodejs-memcached
npm install

Now Create your project server file app.js and include required libraries

var express    = require("express");
var Memcached = require('memcached');
var app = express();
var port = Number(process.env.PORT || 5000);
 
console.log("Listening on "+port+", Web URL: http://localhost:"+port);
app.listen(port);

Create object for your memcached

var memcached = new Memcached();

Add code to connect with your memecahced server

memcached.connect( '127.0.0.1:11211', function( err, conn ){
  if( err ) {
     console.log( conn.server );
  }
});

Create your first memcached key name “profile” and set json data

var profile = {'name':'Rohit kumar', 
               'location':'Delhi', 
               'emailid':'[email protected]' 
               }
// create your profile key where user personal information will be store in json format.               
memcached.set('profile', profile, 10000, function (err) { 
  if(err) throw new err;
});

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

Now your key has been created in memcached server. time to get this key

// get profile key data
memcached.get('profile', function (err, data) {
  console.log(data);
});

You can also delete key at any time by calling this function

// delete profile key
memcached.del('profile', function (err) { 
  if(err) throw new err;
 });

Now your final file will be..

app.js

var express    = require("express");
var Memcached = require('memcached');
var app = express();
var port = Number(process.env.PORT || 5000);
 
// make connection with memcached server
var memcached = new Memcached();
 
memcached.connect( '127.0.0.1:11211', function( err, conn ){
  if( err ) {
     console.log( conn.server );
  }
});
var profile = {'name':'Rohit kumar', 
               'location':'Delhi', 
               'emailid':'[email protected]' 
               }
// create your profile key where user personal information will be store in json format.               
memcached.set('profile', profile, 10000, function (err) { 
  if(err) throw new err;
});
 
 
// get profile key data
memcached.get('profile', function (err, data) {
  console.log(data);
});
 
// delete profile key
/*memcached.del('profile', function (err) { 
  if(err) throw new err;
 });*/
 
console.log("Listening on "+port+", Web URL: http://localhost:"+port);
app.listen(port);

nodejs-memcached-1
These are the very basic function of memcached you can try more function as per your need like

get memcahed multi key value

memcached.getMulti(['key1', 'key2'], function (err, data) {
  console.log(data.key1);
  console.log(data.bkey2);
});

Read memecahed official api documentation for more functions
https://www.npmjs.com/package/memcached

If you like this post please don’t forget to subscribe my public notebook for more useful stuff.