node(redis)
3947 ワード
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
});