Simple Example of Node. js operations on Redis

Source: Internet
Author: User

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

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.