i8xscore
Version:
i8xiaoshi base socket communication,last update at 151021
184 lines (181 loc) • 5.86 kB
JavaScript
var _=require('underscore');
var serverConfig=global.serverConfig;
var redis=require('redis');
var log=require('./log');
//var rClient=redis.createClient(serverConfig.redisPort,serverConfig.redisHost,{connect_timeout:100});
var rClient=redis.createClient(serverConfig.redisNode.port,serverConfig.redisNode.host,serverConfig.redisNode);
rClient.on("connect",function(){
console.log('redis is connected !');
});
rClient.on("error",function(err){
log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
});
exports.setCache=function(key,value,fncallback,expire){
if(key&&value) {
var saveData= _.isObject(value)?JSON.stringify(value):value;
rClient.set(key, saveData, function (err, res) {
if (!err) {
if (fncallback) {
fncallback({result:true,data:res});
}
}else{
log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if (fncallback) {
fncallback({result:false,data:err});
}
}
});
if(expire){
rClient.expire(key,expire);
}
//rClient.end();
}else{
if (fncallback) {
fncallback({result:false,data:'Key or value is null!'});
}
}
};
//
exports.hsetCache=function(key,field,value,fncallback,expire){
if(key&&value) {
var saveData= _.isObject(value)?JSON.stringify(value):value;
rClient.hset(key,field, saveData, function (err, res) {
if (!err) {
if (fncallback) {
fncallback({result:true,data:res});
}
}else{
log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if (fncallback) {
fncallback({result:false,data:err});
}
}
});
if(expire){
rClient.expire(key,expire);
}
//rClient.end();
}else{
if (fncallback) {
fncallback({result:false,data:'Key or value is null!'});
}
}
};
//hash key object
exports.hmultiSetCache=function(hashKey,obj,fncallback,expire){
if(hashKey&&obj) {
rClient.hmset(hashKey,obj,function (err, res) {
if (!err) {
if (fncallback) {
fncallback({result:true,data:res});
}
}else{
log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if (fncallback) {
fncallback({result:false,data:err});
}
}
});
if(expire){
rClient.expire(hashKey,expire);
}
//rClient.end();
}else{
if (fncallback) {
fncallback({result:false,data:'Key or value is null!'});
}
}
};
//全部指定hash对列
exports.hGetAllCache=function(key,fncallback){
if(key) {
rClient.hgetall(key,function (err, res) {
if (!err&&res) {
var newData = null;
try {
newData = res;
} catch (ex) {
newData = res;
}
if (fncallback) {
fncallback({result: true, data: newData});
}else{
return;
}
}else {
//log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if(fncallback){
fncallback({result:false,data:res});
}else{
return;
}
}
});
//rClient.end();
}else{
fncallback({result:false,data:'No key'});
}
}
exports.hGetCache=function(key,field,fncallback){
if(key) {
rClient.hget(key,field, function (err, res) {
if (!err&&res) {
var newData = null;
try {
newData = JSON.parse(res);
} catch (ex) {
newData = res;
}
if (fncallback) {
fncallback({result: true, data: newData});
}else{
return;
}
}else {
//log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if(fncallback){
fncallback({result:false,data:res});
}else{
return;
}
}
});
//rClient.end();
}else{
fncallback({result:false,data:'No key'});
}
}
exports.getCache=function(key,fncallback){
if(key) {
rClient.get(key, function (err, res) {
if (!err&&res) {
var newData = null;
try {
newData = JSON.parse(res);
} catch (ex) {
newData = res;
}
if (fncallback) {
fncallback({result: true, data: newData});
}else{
return;
}
}else {
//log.logger.warn('[cache.error]缓存错误:error,'+JSON.stringify(err));
if(fncallback){
fncallback({result:false,data:res});
}else{
return;
}
}
});
//rClient.end();
}else{
fncallback({result:false,data:'No key'});
}
}
exports.clearCache=function(key,fncallback){
if(key){
rClient.del(key);
}
}