npm install redis
官网:
https://github.com/NodeRedis/node_redis
代码:
连接
var redis = require("redis");
var client = redis.createClient(6379,'192.10.200.105',{connect_timeout:1});
client.info(function(err,response){
console.log(err,response);
});
client.on('error',function(error){
console.log(error);
});
multi
在遇到exec()之前都不会运行里面的程序。在exec()时,上面代码会按队列执行。
// multi chain with an individual callback
client.multi()
.scard("bigset")
.smembers("bigset")
.keys("*", function (err, replies) {
// NOTE: code in this callback is NOT atomic
// this only happens after the the .exec call finishes.
client.mget(replies, redis.print);
})
.dbsize()
.exec(function (err, replies) {
console.log("MULTI got " + replies.length + " replies");
replies.forEach(function (reply, index) {
console.log("Reply " + index + ": " + reply.toString());
});
});
redis.print()
测试时使用的一个回调函数
var redis = require("redis"),
client = redis.createClient();
client.on("connect", function () {
client.set("foo_rand000000000000", "some fantastic value", redis.print);
client.get("foo_rand000000000000", redis.print);
});
mget
var client = require("redis").createClient();
client.mget(["sessions started", "sessions started", "foo"], function (err, res) {
console.dir(res);
});
键值是否存在
client.exists('key', function(err, reply) {
if (reply === 1) {
console.log('exists');
} else {
console.log('doesn\'t exist');
}
});