/** * Created by chen on 2017/8/21. */ let Redis = require('ioredis'), config=require('./config'), redisConfig = config.current.redis; redis = new Redis({ port: redisConfig.port, // Redis port host: redisConfig.server, // Redis host family: 4, // 4 (IPv4) or 6 (IPv6) password:redisConfig.pwd, db: 0 }); let client={}; redis.on('ready',function(res){ console.log('ready'); }); redis.on("error", function (err) { console.log("Error " + err); }); client.redis = redis; //redis.hmset('测试',{12525:55,'a':'aaaa',b:"bbbbb"}); //var stream = redis.scanStream({ match: '测*'}); /*redis.hmset('RATION',{12525:55,'a':'aaaa',b:"bbbbb"}); redis.hgetall("RATION",function (err,result) { console.log(result); })*/ client.scan = function(options,callback){ var stream = null; if(options){ stream = redis.scanStream(options); }else { stream = redis.scanStream(); } var keys = []; stream.on('data', function (resultKeys) { // `resultKeys` is an array of strings representing key names for (var i = 0; i < resultKeys.length; i++) { keys.push(resultKeys[i]); } }); stream.on('end', function () { console.log('done with the keys: ', keys); callback(keys); }); } client.set = function(key, value, expire, callback){ return redis.set(key, value, function(err, result){ if (err) { console.log(err); callback(err,null); return; } if (!isNaN(expire) && expire > 0) { redis.expire(key, parseInt(expire)); } if(callback){ callback(null,result); } }) } client.get = function(key, callback){ return redis.get(key, function(err,result){ if (err) { console.log(err); callback(err,null); return; } if(callback){ callback(null,result); } }); } client.hmset = function(key, value, expire, callback){ return redis.hmset(key, value, function(err, result){ if (err) { console.log(err); callback(err,null); return; } if (!isNaN(expire) && expire > 0) { redis.expire(key, parseInt(expire)); } if(callback){ callback(null,result); } }) } client.hmget = function (key,fields,callback) { return redis.hmget(key,fields,function(err,result){ if (err) { console.log(err); callback(err,null) return; } if(callback){ callback(null,result); } }) } client.hgetall = function (key,callback) { return redis.hgetall(key,function (err,result) { if (err) { console.log(err); callback(err,null) return; } callback(null,result); }) } client.hscan = function (key,options) { var stream = null; if(options){ stream = redis.hscanStream(key,options); }else { stream = redis.hscanStream(key); } var fields = []; stream.on('data', function (resultFields) { // `resultKeys` is an array of strings representing key names for (var i = 0; i < resultFields.length; i++) { fields.push(resultFields[i]); } }); stream.on('end', function () { console.log('done with the keys: ', fields); callback(fields); }); } client.rpush = function(key,value,expire, callback){ return redis.rpush(key, value, function(err, result){ if (err) { console.log(err); callback(err,null); return; } if (!isNaN(expire) && expire > 0) { redis.expire(key, parseInt(expire)); } if(callback){ callback(null,result); } }) } client.lrange= function (key,start,end,callback) { return redis.lrange(key,start,end,callback);//获取列表在给定范围上的所有值 array lrange('key', 0, -1) (返回所有值) } client.lindex =function (key,index,callback) { return redis.lindex(key,index,callback);//获取列表在给定位置上的单个元素 lindex('key', 1) } client.lpop = function (key,callback) { return redis.lpop(key,callback);//从列表左端弹出一个值,并返回被弹出的值lpop('key') } client.rpop = function (key,callback) { return redis.rpop(key,callback);//从列表右端弹出一个值,并返回被弹出的值rpop('key') } client.ltrim = function (key,start,end,callback) { return redis.ltrim(key,start,end,callback);//将列表按指定的index范围裁减 ltrim('key', 'start', 'end') } client.del = function (keys,callback) { return redis.del(keys,callback);// 删除一个(或多个)keys return 被删除的keys的数量 del('key1'[, 'key2', ...]) } client.exists = function (key,callback) { return redis.exists(key,callback);// 查询一个key是否存在 1/0 exists('key') } client.multi = function () { return redis.multi();//用于开启一个事务,它总是返回 OK 。 } client.exec =function () { return redis.exec();//执行所有事务块内的命令 } client.application_init = function () { redis.flushdb();//清空所有keys // load datas -- to do /*data=[{ method:'hmset', key:'ration:25555', value:{ name:'aaa', quantily:255 }, expire:2000,//options callback:function, //option }] for(let data of datas){ client[data.method](data.key,data.value,data.expire,data.callback); } */ } module.exports = client