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:
?
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 |