A Simple Method for reading and writing Redis databases in Node. js applications
This article mainly introduces a simple method for reading and writing Redis databases in Node. js applications. Redis is a memory-based high-speed database. For more information, see
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:
?
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. 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:
?
You can also use the EXPIRE command to set the object expiration time. The Code is 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 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:
?
1 2 3 4 5 |
Reply: OK I live: Hello World I live: Hello World I live: Hello World I expired |
Next we will check how long a value remains 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 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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:
?
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 World I live for this long yet: 0 I expired |