a blog for those who code

Saturday, 19 July 2014

Use of Redis in Nodejs

In this post we are going to discuss and learn about Redis in NodeJS. Redis is useful when you are doing a lot of writing as well as reading of data, it can be persisted and it provides more flexibility because it has a support for different types of data.

The redis module can be installed using npm.

npm install redis

To use redis in your Node applications, you have to include the module, which can be done using the below command.

var redis = require('redis');

To create a client in redis, you have to use the below command.

var client = redis.createClient();

This above createClient() method takes three optional parameters : port,host,options. By default, the host is set to and the port is set to 6379.

The third parameter is an object that supports the following options :

parser : Reply parser, by default its hiredis. You can also use javascript.
return_buffers : By default false, if its true all replies are set as Node buffer rather than strings.
no_ready_check : By default false, if its true "ready check" sent to server to check if its is ready for more commands.
detect_buffers : By default fasle, if its true all replies are sent as buffer objects.
socket_nodelay : By default true, it tells whether to call setNoDelay on TCP stream.

Its good to leave all the options as default, but if you want to play with these values to see whats the difference, just go on and change values.

Now if you have created a connection with the client, you can send several commands until you explicitly call client.quit method. You can also call client.end method, but its good to call client.end method if your application is stuck or you want to start over.

The redis.print method prints either the error or the reply to the console and returns.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment