How to use Redis in Node JS

How to use Redis in Node JS

What is Redis?

Redis is an in-memory data structure store which may be used as a database, a cache, and a message broker. Redis supports different data structures like strings, lists, sets, hashes, bitmaps and etc. Redis uses your RAM to store data which is extremely fast, however, if you reboot your server the values are gone unless you enable Redis persistence. By default, Redis enables persistence mechanism for you (you can disable or configure persistence consistent with your needs)

Install Redis (Linux):

Refer: https://redis.io/download

Download, extract and compile Redis with

$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz

$ tar xzf redis-4.0.9.tar.gz

$ cd redis-4.0.9

$ make

The binaries that are now compiled are available within the src directory. Run Redis with:

$ src/redis-server

You can communicate with Redis using the built-in client

$ src/redis-cli

redis> set foo bar

OK

redis> get foo

“bar”

now we’ve successfully installed Redis on the local machine

Using Redis in your NodeJS application:

First, you must install Redis client for NodeJS via npm.

npm install redis

Now create a file referred to as redisDemo.js in your NodeJS project.

// redisDemo.js

var redis = require(‘redis’);

var client = redis.createClient(); // this creates a new client

By default redis.createClient() use 127.0.0.1 and port 6379. If you’ve got a customized IP and a port use

var client = redis.createClient(port, host);

Now, we would like to concentrate on the connect event to ascertain whether we successfully connected to the redis-server. we will check for a successful connection like this.

client.on(‘connect’, function() {

console.log(‘Redis client connected’);

});

Likewise, we will check if we failed to connect to the redis-server. Well, we will hear the error event for that.

client.on(‘error’, function (err) {

console.log(‘Something went wrong ‘ + err);

});

This might trigger once you forget to start out the redis-server before the appliance is run. So confirm to run the redis server before testing this code.

You can start, stop the redis server using the subsequent commands.

/etc/init.d/redis-server stop

/etc/init.d/redis-server start

Let’s see how our code looks like.

var redis = require(‘redis’);

var client = redis.createClient();

client.on(‘connect’, function() {

console.log(‘Redis client connected’);

});

client.on(‘error’, function (err) {

console.log(‘Something went wrong ‘ + err);

});

Now, Let’s see the way to set some simple value under a key in redis. you’ll use set() and get() methods for that.

client.set(‘my test key’, ‘my test value’, redis.print);

client.get(‘my test key’, function (error, result) {

if (error) {

console.log(error);

throw error;

}

console.log(‘GET result ->’ + result);

});

in client.set() we first give the key then value. Redis may be a key-value store. Redis can produce a key named ‘my check key’ and assign the value ‘my check value’ for that key.

Here we use redis.print on the set() method. It prints “Reply: OK” to the console by saying that redis saved the value. you’ll omit that argument if you would like.

Now in get() method we simply retrieve the value we just saved by specifying the precise key name. Then it’ll print the saved value within the console.

Let’s see the entire code now.

var redis = require(‘redis’);

var client = redis.createClient();

client.on(‘connect’, function() {

console.log(‘Redis client connected’);

});

client.on(‘error’, function (err) {

console.log(‘Something went wrong ‘ + err);

});

client.set(‘my test key’, ‘my test value’, redis.print);

client.get(‘my test key’, function (error, result) {

if (error) {

console.log(error);

throw error;

}

console.log(‘GET result ->’ + result);

});

The output will look like this

Redis client connected

Reply: OK

GET result ->my test value

Conclusion: Redis is extremely powerful in-memory data-store that we will use in our applications. It’s very simple to save lots of and obtain data without much overhead.

For More information about Node JS Click Here

Spread Your Love & Share It.

Leave a Reply

Your email address will not be published. Required fields are marked *