redis

var redis = require('redis'),
RDS_PORT = 6379, //端口号
RDS_HOST = '192.168.1.125', //服务器IP
RDS_OPTS = {}; //设置项
var client = redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS);
client.on("error", function(err){
console.log("Error: " + err);
});
client.on('ready',function(res){
console.log('ready');
});
client.on("connect", function(){
//key,value
client.set("name_key", "hello world", function(err, reply){
console.log(reply.toString());
});
client.get("name_key", function(err, reply){
console.log(reply.toString());
});


//json 自动转成json
//client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
//client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
//client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
client.hmset('sessionid', { username: 'kris', password: 'password' }, function(err) {
console.log(err)
})
client.hgetall('sessionid', function(err, object) {
console.log(object)
})
client.keys('session*', function (err, keys) {
console.log(keys);
})

//[]
client.hset("hash key", "hashtest 1","");
client.hset(["hash key", "hashtest 2",""]);
client.hkeys("hash key", function (err, replies) {
console.log(replies);
})
client.incr('uid');
client.get("uid", function(err, reply){
console.log(reply);
});
client.get('uid',function(err,uid){
//建立索引集合,username--id
var username="rjy";
var password="rjy";
client.set('username:'+username,uid);
client.hmset("user:"+uid,'username',username,'password',password,function(err){
if(err){
return 0;
}
else{
return 1;
}
});
});

client.keys('*', function (err, keys) {
console.log(keys);
})
/*

username:zhou=>1
user:1=>{username='zhou',password='123'}


client.get('username:'+username,function(err,uid){
if(err){
return callback(err);
}
client.hget('user:'+uid,'password',function(err,pass){
client.get("uid", function(err, reply){
console.log(reply);
});
console.log("--AA--");
*/
//
//handle_regist("rjy","rjy");
// client.incr('uid');

})
/*
exports.handle_regist = function(username,password){
client.incr('uid');
client.get('uid',function(err,uid){
//建立索引集合,username--id
client.set('username:'+username,uid);
client.hmset("user:"+uid,'username',username,'password',password,function(err){
if(err){
return 0;
}
else{
return 1;
}
});
});
};*/

你可能感兴趣的:(redis)