Redis is a key-value database, and all keys are strings. values can be set, hash, list, and so on.
Redis uses the MULTI, DISCARD, EXEC, and WATCH commands to implement transaction functions. For transactions, we must know that transaction security is very important.
A transaction provides a mechanism to "Package Multiple commands and then execute them in one time and in order" without interrupting the execution of the transaction-that is, before the transaction is completed, other commands on the client are blocked.
Var redis = require ("redis"); var client = redis. createClient (); client. on ("error", function (err) {console. log ("Error" + err) ;}); client. on ("connect", runSample); function runSample () {// set a value of the string type, return value: OKclient. set ("string key", "Hello World", function (err, reply) {console. log (reply. toString () ;}); // gets a value of the string type. The returned word is valueclient. get ("string key", function (err, reply) {console. log (reply. toString () ;}); // you can use another method to obtain a value of the string type. The returned word is valuevar multiCmd = client. multi (); multiCmd. get ("string key" contains multiple multicmd.exe c (function (err, reply) {console. log (reply. toString () ;}); // set the expiration time to client. expire ('string key', 3); // validate 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); // check how long a value remains before it expires 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); // sets var key = "set key"; client. sadd (key, uid); client. sadd (key, "a"); client. sadd (key, "B"); // obtain whether the key set contains "1". If yes, 1 is returned. Otherwise, 0 client is returned. sismember (key, "a", showData); // client. quit ();} function writeTTL (err, data) {console. log ("I live for this long yet:" + data);} function showData (err, data) {if (err) {console. log ("err:" + err);} else {console. log ("reply:" + data );}}
References:
Redis command reference
Read and Write Redis data (expiration time and TTL) in Node. js)
Github node_redis
Redis Design and Implementation
Document Information
- Copyright statement: Free Reprint-non commercial-Non derivative-keep the signature | Creative Commons BY-NC-ND 3.0
- Http://blog.csdn.net/cdztop/article/details/32784295
- Last modification time: June 21, 2014