Open In App

Node.js Redis Module

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 is in your system:

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

Getting Started: First, initialize the application with the 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 app.js. You can name your file whatever you want. In the app.js file, write the following code:

Filename: app.js 

javascript




const redis = require("redis");
let 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 the app.js file: 

javascript




const redis = require("redis");
let 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 an error (if any) or it will log the value as OK if the 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:

javascript




const redis = require("redis");
let 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: 

javascript




const redis = require("redis");
let 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:

javascript




const redis = require("redis");
let 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 database storage for various data structures.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads