A Simple Method for reading and writing Redis databases in Node. js applications, node. jsredis

Source: Internet
Author: User

A Simple Method for reading and writing Redis databases in Node. js applications, node. jsredis

Before starting this article, make sure that you have installed Redis, Node. js, and Node. js Redis extensions-node_redis

First, create a new folder and create a text file app. js. The content of the file is as follows:
 

var redis = require("redis")  , client = redis.createClient(); client.on("error", function (err) {  console.log("Error " + err);}); client.on("connect", runSample); function runSample() {  // Set a value  client.set("string key", "Hello World", function (err, reply) {    console.log(reply.toString());  });  // Get a value  client.get("string key", function (err, reply) {    console.log(reply.toString());  });}

After connecting to Redis, The runSample function is called and a value is set. Then the value is read. The running result is as follows:
 

OKHello World

You can also use the EXPIRE command to set the object expiration time. The Code is as follows:
 

var redis = require('redis')  , client = redis.createClient(); client.on('error', function (err) {  console.log('Error ' + err);}); client.on('connect', runSample); function runSample() {  // Set a value with an expiration  client.set('string key', 'Hello World', redis.print);  // Expire in 3 seconds  client.expire('string key', 3);   // This timer is only to demo the TTL  // Runs every second until the timeout  // occurs on the value  var myTimer = setInterval(function() {    client.get('string key', function (err, reply) {      if(reply) {        console.log('I live: ' + reply.toString());      } else {        clearTimeout(myTimer);        console.log('I expired');        client.quit();      }    });  }, 1000);}

Note: The timer used above is only used to demonstrate the EXPIRE command. You must exercise caution when using the timer in the Node. js project.

The output result of running the above program is:

 

Reply: OKI live: Hello WorldI live: Hello WorldI live: Hello WorldI expired

Next we will check how long a value remains before it expires:
 

var redis = require('redis')  , client = redis.createClient(); client.on('error', function (err) {  console.log('Error ' + err);}); client.on('connect', runSample); function runSample() {  // Set a value  client.set('string key', 'Hello World', redis.print);  // Expire in 3 seconds  client.expire('string key', 3);   // This timer is only to demo the TTL  // Runs every second until the timeout  // occurs on the value  var myTimer = setInterval(function() {    client.get('string key', function (err, reply) {      if(reply) {        console.log('I live: ' + reply.toString());        client.ttl('string key', writeTTL);      } else {        clearTimeout(myTimer);        console.log('I expired');        client.quit();      }    });  }, 1000);} function writeTTL(err, data) {  console.log('I live for this long yet: ' + data);}

Running result:
 

Reply: OKI live: Hello WorldI live for this long yet: 2I live: Hello WorldI live for this long yet: 1I live: Hello WorldI live for this long yet: 0I expired

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.