cnpm install node-redis –save
var redis = require('redis');
var client = redis.createClient("6379","127.0.0.1",{auth_pass: "yoursecret"});
client.on("error", function (err) {
console.log("redis client连接失败",err);
});
client.on('ready', function (res) {
console.log('client ready');
});
client.on('connect', function () {
client.hset("hash key", "field 1", "v1", redis.print);
client.hset(["hash key", "field 2", "v2"], redis.print);
client.hget("hash key", "field 1", redis.print);
client.hgetall("hash key",function (err, items) {
console.log(err,items);
});
client.hmset("hash key 2", "field3", "3", "field4", "4");
client.hgetall("hash key 2", function (err, obj) {
console.log("hgetall:",obj);
});
client.hmset("hash key 3", {
"012345": "abcdefghij", // NOTE: key and value will be coerced to strings
"field5": "5"
});
client.hgetall("hash key 3", function (err, obj) {
console.log("hgetall:",obj);
});
client.hkeys("hash key", function (err, replies) {
console.log("hkeys:",err,replies);
replies.forEach(function (reply, i) {
console.log(" " + i + ": " + reply);
});
});
//client.quit();
});
client.on("error", function (err) {
console.log("Error " + err);
});