Tutorial on using Node. js and Sails redis components

  • 2021-07-21 05:58:55
  • OfStack

I haven't written an article about NodeJs in a while. Today is also in order to solve the problem of high concurrency, and think of this thing, IIS site concurrency reached 200 when there is a bottleneck, so thought of this high concurrency support better framework, nodeJs before I wrote a number of articles, mainly for sails framework, introduced a number of usage, today mainly say the next redis components!

Project: SailsMvc

Development tool: webstorm

Language: nodejs

Framework: sails

Package: redis

This paper mainly introduces several usages, such as string, set, hash and list

Test the code of redis component


  index: function (req, res) {
    // redis  Link 
    var redis  = require('redis');
    var client = redis.createClient('6379', '127.0.0.1');
    // redis  Link error 
    client.on("error", function(error) {
      console.log(error);
    });
    //redis list Use 
    client.lpush('ok', 'Hello World!', function(error, res) {
      if(error) {
        console.log(error);
      } else {
        console.log(res);
      }
    });
    // redis  Validation  (reids.conf Authentication is not turned on, this item is not required )
       client.auth("foobared");
    // Select the database and use the set Structure 
    client.select('0', function(error){
      if(error) {
        console.log(error);
      } else {
        // set
        client.set('str_key_0', '0', function(error, res) {
          if(error) {
            console.log(error);
          } else {
            console.log(res);
          }
        });
      }
    });
    // Use hash Structure 
    client.hmset("nodejs","zzl01","OK", function(error, res) {
      if (error) {
        console.log(error);
      } else {
        console.log(res);
      }
    });
    // Close the connection 
    client.end();
    return res.send("OK");
    //return res.view("view_name",data)//view_name Parameter is empty to indicate that the current action
  }

Related articles: