node(redis)

给出node下redis操作的简单例子:

var redis = require("redis"),
client = redis.createClient(6379,'127.0.0.1',{});  

client.on("error", function (err) {  
    console.log("Error " + err);  
});  

//设置并获取key1的值
client.set("key1", "val1", redis.print);
client.set("key1", "val2", redis.print);
client.get("key1", function(err, reply) {
    console.log(reply);//打印结果val2
});

client.mset(["key2", "val2", "key3", "val3"], function (err, res) {
    client.get("key3", function(err, reply) {
        console.log(reply);//打印结果val3
    });
});

//在名为hosts的hash表中添加数据
client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
client.hgetall("hosts", function (err, obj) {
    console.log(obj);//打印结果{"mjr", "1", "another", "23", "home", "1234"}
});


client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
//返回hash key中的keys
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
});

client.multi([
              ["mset", "something1","1", "something2","1", redis.print],
              ["incr", "something1"],//值加1
              ["incr", "something2"]//值加1
          ]).exec(function (err, replies) {
              console.log(replies);//打印['OK',2,2]
              
          });

client.get("something1", function (err, obj) {
    console.log(obj);//打印2
});
原文地址:https://www.cnblogs.com/Fredric-2013/p/4519424.html