Skip to content
Related Articles

Related Articles

Improve Article

Node.js | Redis Module

  • Last Updated : 16 Jul, 2020

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




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

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:




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);
});

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:




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); 
});

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:




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" });

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:




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);
  });

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
Recommended Articles
Page :