Node.js | Redis Module

Redis is an Open Source store for storing data structures. It is used in multiple ways. It is used as a database, cache, and message broker. It can store data structures such as strings, hashes, sets, sorted sets, bitmaps, indexes, and streams.

Redis is very useful for Node.js developers as it reduces the cache size which makes the application more efficient. However, it is very easy to integrate Redis with Node.js applications. 

Prerequisites: Before starting with our application, make sure the following installation in your system:

  1. An IDE of your choice.
  2. Redis installed in your system. Check the version that you are using the following command:
    redis cli --version
  3. Node.js, NPM installed and configured in your system.
  4. Basic knowledge of Node.js and Redis.

Getting Started: First initialize the application with package.json file. Type the following command:

npm init

Now, install the redis module by the following command:



npm install redis --save

Now, create a new file and name it as app.js. You can name your file whatever you want. In app.js file, write the following code:

Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

var redis = require("redis");
var client = redis.createClient();
  
client.on("connect", function() {
  console.log("Connection Successful!!");
});

chevron_right


Here, we are importing the redis module and then creating a client for redis. The client is then used to manipulate the module. In the above code, we are creating a server. To run the application just type the following command:

node app.js

The output for the above code will be logged in the console as:

Connection Successful!!

Storing of String: To store a string in Redis, write the following code in app.js file:

filter_none

edit
close

play_arrow

link
brightness_4
code

var redis = require("redis");
var client = redis.createClient();
  
client.on("connect", function() {
  console.log("Connection Successful!!");
});
  
client.set("Intern", "gfg", (err, stu) => {
    if (err) console.log(err);
    else console.log(stu);
});

chevron_right


The client.set() function takes the key-value format. Here, Intern is the key and gfg is the value. Further, it takes a callback function which will return error (if any) or it will log the value as OK if string is successfully stored. The output for the above code will be:

Connection Successful!!
OK

To get the value of the key stored in the redis database, we will use the get function as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

var redis = require("redis");
var client = redis.createClient();
  
client.on("connect", function() {
  console.log("Connection Successful!!");
});
  
client.set("Intern", "gfg", (err, stu) => {
    if (err) console.log(err);
    else console.log(stu);
});
  
client.get('Intern', (err, stu) => {
    if (err) console.log(err);
    else console.log(stu); 
});

chevron_right


Here, client.get() method will get the value of the key Intern and log it in the console. The output for the above code will be:

Connection Successful!!
OK
gfg

Storing Objects: To store objects in redis database, write the following code:

filter_none

edit
close

play_arrow

link
brightness_4
code

var redis = require("redis");
var client = redis.createClient();
  
client.on("connect", function() {
  console.log("Connection Successful!!");
});
  
client.set("Intern", "gfg", (err, stu) => {
    if (err) console.log(err);
    else console.log(stu);
});
  
client.get('Intern', (err, reply) => {
    console.log(reply); 
});
  
client.hmset("Interns", { pos: "Tech Writer", Org: "GFG" });

chevron_right


Now, the key will be Interns and its value will be { pos: “Tech Writer”, Org: “GFG” }.

To get the value from the redis database, write the following code:

filter_none

edit
close

play_arrow

link
brightness_4
code

var redis = require("redis");
var client = redis.createClient();
  
client.on("connect", function() {
  console.log("Connection Successful!!");
});
  
client.set("Intern", "gfg", (err, stu) => {
    if (err) console.log(err);
    else console.log(stu);
});
  
client.get('Intern', (err, reply) => {
    console.log(reply); 
})
  
client.hmset("Interns", { pos: "Tech Writer", Org: "GFG" });
  
client.hgetall("Interns", (err, stu) => {
    if (err) console.log(err);
    else console.log(stu);
  });

chevron_right


Here, hgetall method is used to get all values of the key and log it in the console. The output for the above code will be:

Connection Successful!!
OK
gfg
{ pos: 'Tech Writer', Org: 'GFG' }

Conclusion: Redis can make Node.js applications fast, powerful, and efficient. Redis can be called as database storage for various data structures.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.