A simple example of node. JS operating Redis

Source: Internet
Author: User

Redis is a key-value type of database, and key is all strings, value can be a collection, hash, list, and so on.


Redis implements transactional functionality through the Multi/discard/exec/watch of these 4 commands. For transactions, we must know that transaction security is a very important one.


Transactions provide a mechanism for packaging multiple commands and then executing them one at a time, and will not break during transaction execution-meaning that the client's other commands are blocked until the transaction is complete.


var Redis = require ("Redis"), var client = Redis.createclient (); Client.on ("Error", function (Err) {Console.log ("error" + E RR);}); Client.on ("Connect", runsample); function runsample () {//Set a value of string type, return value: Okclient.set ("String Key", "Hello World", function (err, reply) {Console.log (reply.tostring ());}); /Gets the value of a string type, returns the word: Valueclient.get ("string Key", function (err, reply) {Console.log (reply.tostring ());}); /Another way to get the value of a string type, return Word: Valuevar multicmd = Client.multi (), Multicmd.get ("string Key"), Multicmd.exec (function (Err, Reply) {Console.log (reply.tostring ());}); /Set Expiration Time Client.expire (' string key ', 3);//valid time validation 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 is persisted 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);//set operation var key = "Set key"; Client.sadd (Key, UID); Client.sadd (Key, "a"), Client.sadd (Key, "B");//Gets the key collection contains "1", if included, returns 1, otherwise returns 0client.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);}}

Resources:


Redis Command Reference

Read and write Redis data in node. JS (Expiration Time and TTL)

Github Node_redis
Redis Design and implementation


Document Information

    • Copyright Disclaimer: Free Reprint-Non-commercial-non-derivative-retain attribution | Creative Commons by-nc-nd 3.0
    • Original URL: http://blog.csdn.net/cdztop/article/details/32784295
    • Last modified: June 21, 2014 02:07

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.