Introduction to the method of using Redis in Node.js applications

Source: Internet
Author: User
Tags redis require

Before you begin this article, be sure to install the Redis extensions for Redis and Node.js and Node.js--node_redis

First create a new folder and new text file app.js the contents of the file are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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.to String ()); }); Get a value Client.get ("String Key", function (err, reply) {Console.log (reply.tostring ());}); }

When connected to the Redis, the Runsample function is called and a value is set, followed by a readout of the value, running as follows:

?

1 2 OK Hello World

We can also use the EXPIRE command to set the expiration time of an object, as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 the Expiration Client.set (' string key ', ' Hello World ', redis.print);//Expire I   N 3 seconds Client.expire (' string key ', 3); This is the ' only ' to Demo ' TTL//runs every second until the timeout//occurs on the value var MyTimer = Setinter Val (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 to demonstrate the EXPIRE command, you must use the timer carefully in the Node.js project.

The output from running the above program is:

?

1 2 3 4 5 Reply:ok i Live:hello World i live:hello World I live:hello world I expired

Next we check how long a value is persisted before it expires:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The 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 are only to demo the TTL//runs every second until the timeout//occurs on the value var MyTimer = s Etinterval (function () {client.get (' string key ', function (err, reply) {if (reply) {Console.log (' I live: ' + Reply.tostri Ng ()); 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); }

Run Result:

?

1 2 3 4 5 6 7 8 Reply:ok i Live:hello world I live for this long yet:2 I Live:hello World I live for this long yet:1 i live:hello Wo Rld I live for this long yet:0 I 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.