UNPKG

doomi-helper

Version:

Doomisoft NodeJs Common Utilities

320 lines (307 loc) 9.81 kB
/** * Redis常用功能封装 * 通过co-redis将redis操作同步化 */ var redis = require('redis'); var wrapper = require('co-redis'); const fs = require('fs'); const path = require('path') class RedisUtil{ /** * 通过从config文件中的配置,构造出redis访问实例 * @param dbPosition */ // constructor(dbPosition){ // let config = process.env.CONFIGFILE || 'configuration.json'; // let configfile = path.join(process.cwd(), config); // if (!fs.existsSync(configfile)) throw Error('missing configuration file') // this.config = require(configfile); // if (!this._redisCo && this.config.redisConnections){ // console.log('create new client') // if (dbPosition==null) dbPosition = this.config.redisConnections.db || 0; // this.initClient(dbPosition, this.config.redisConnections); // } // } constructor(redisOption) { this.redisoption = redisOption || {}; if (!this._redisCo) { console.log('create new client') // if (dbPosition == null) dbPosition = this.config.redisConnections.db || 0; this.initClient(redisOption); } } /** * 单例模式 */ static getInstance(){ if (!RedisUtil.redis) { let configuraton = process.env.CONFIGFILE || 'configuration.json'; let configfile = path.join(process.cwd(), configuraton); if (!fs.existsSync(configfile)) throw Error('missing configuration file') let config = require(configfile); RedisUtil.redis = new RedisUtil(config.redisConnections); } return RedisUtil.redis; } /** * 单例模式 */ static getInstanceByName(namespace,option) { if (!RedisUtil.NameRedis || !RedisUtil.NameRedis[namespace]) { if (!RedisUtil.NameRedis){ RedisUtil.NameRedis = {}; } RedisUtil.NameRedis[namespace] = new RedisUtil(option); } return RedisUtil.NameRedis[namespace]; } /** * 返回redis的客户端对象 */ getRedisClient(){return this.redisClient;} /** * 返回redis的链接配置信息 */ getRedisConfig() { return this.redisoption;} /** * * @param {*} dbPosition */ initClient(option){ // console.log('redis config is:'+JSON.stringify(this.config.redisConnections)); this.dbPosition = option.db; //dbPosition; this.redisClient = redis.createClient(option) // redis.createClient(this.config.redisConnections); this.redisClient.select(option.db || 0); this._redisCo = wrapper(this.redisClient); this.messageProcessHandler = []; //redis连接错误 this.redisClient.on('error', function (err) { console.log(err); }); } getDbIndex(){ return this.dbPosition ; } ///订阅键值过期消息消息 subscribe(redisInst,channel,messageHandler){ let _instance = redisInst; _instance.redisClient.subscribe(channel); if (messageHandler) _instance.messageProcessHandler[channel] = messageHandler; _instance.redisClient.on("message", function (channel, message) { console.log("我接收到信息了" + message); if (_instance.messageProcessHandler[channel]){ _instance.messageProcessHandler[channel].call(_instance,message); } }); _instance.redisClient.on("subscribe", function (channel, count) { console.log("client subscribed to " + channel + "," + count + "total subscriptions"); }); } /** * 退出redis,取消监听 * @param {*} channel */ quit(channel){ if (this.redisClient){ if (channel) this.redisClient.unsubscribe(channel) this.redisClient.quit(); } } /* * 删除key */ delete(key){ return this._redisCo.del(key); } /** * 批量删除满足条件的Key * @param {*} pattern */ deleteKeyPattern(pattern){ return this._redisCo.keys(pattern) .then(keyValues=>{ if (keyValues && Array.isArray(keyValues) && keyValues.length){ return this._redisCo.del(keyValues); } }) } /* * 对key设置过期时间 */ expire(key,second){ return this._redisCo.expire(key, second); } ///设置Redis的单个Key值 set(key, value, expire) { //if (expire && expire < 1) return Promise.reject(new Error("过期时间不能小于一秒")); let serilizeValue = typeof(value)=="string"?value:JSON.stringify(value); if (!expire) return this._redisCo.set(key, serilizeValue); return this._redisCo.setex(key, expire, serilizeValue) } /**获取redis string */ get(key) { return this._redisCo.get(key); } //自动增长 increase(key,step=1) { return this._redisCo.incrby(key,step); } //递减 decrease(key,step=1) { return this._redisCo.decrby(key,step); } ///设置redis hash hmset(key, ...args) { return this._redisCo.hmset(key, ...args); } ///获取redis hash hmget(key, ...args) { return this._redisCo.hmget(key,...args); } /** * 设置MapSet的值 */ hset(key,field,object){ return this._redisCo.hset(key,field,typeof(object)=="string"?object:JSON.stringify(object)); } /** * 自动增长 * @param {*} key * @param {*} field * @param {*} step */ hincrease(key,field,step){ return this._redisCo.hincrby(key,field,step) } /** * 清空当前数据 */ flushDB(){ return this._redisCo.flushdb(); } /** * 获取当前DB的key de 大小 */ getDBSize(){ return this._redisCo.dbsize(); } /** * 获取MapSet中的一个键值 * @param {*} key * @param {*} field */ hget(key,field){ return this._redisCo.hget(key,field); } /** * 获取键值的三列数量 * @param {*} key * @param {*} field */ hlen(key){ return this._redisCo.hlen(key); } // ///获取redis hash 子值 // hmgetsub(key, smallkey) { // return this._redisCo.hget(key, smallkey); // } ///删除整个HASH或子值 hdel(key, field) { return this._redisCo.hdel(key, field); } /** * 获取一个hash表下的所有类型 * @param {*} key * @param {*} type type=0,返回key value对 type=1 仅返回key type=2 金返回value */ hgetall(key,type=0){ if(type==0) return this._redisCo.hgetall(key); if (type==1) return this._redisCo.hkeys(key); if (type==2) return this._redisCo.hvals(key); } ///添加ordered set 有序集合 setOSValue(key,score,value){ if(typeof(value)=="object"){ return this._redisCo.zadd(key,score,JSON.stringify(value)); } return this._redisCo.zadd(key,score,value); } /** * 删除ordered set中的元素 */ deleteOSKey(key,value){ return this._redisCo.zrem(key,typeof(value)=="string"?value:JSON.stringify(value)); } getOSValueDesc(key,maxScore,minScore){//,limitCount,offset,count){ return this._redisCo.zrevrangebyscore(key,maxScore || '+inf' ,minScore || '-inf'); } /** * 从有序集合中获取按分值排名的前 * @param {*} key * @param {*} start * @param {*} length */ getOSRankDesc(key,length){ return this._redisCo.zrevrange(key,0,length-1); } getOSLength(key){ return this._redisCo.zcard(key); } /* * 获取到值在order set中的分数 */ getOSScore(key,value){ if(typeof(value)=="object"){ console.log('value is object'); return this._redisCo.zscore(key,JSON.stringify(value)); } return this._redisCo.zscore(key,value); } /* * 递增某个项的值 * */ increaseOSScore(key,value,increaseValue){ if(typeof(value)=="object"){ console.log('value is object'); return this._redisCo.zincrby(key,increaseValue,JSON.stringify(value)); } return this._redisCo.zincrby(key,increaseValue,value); } ///从list对象中获取一个元素 poplistValue(key){ return this._redisCo.lpop(key); } ///设置List setlistValue(key,value,direction='left'){ let realvalue = typeof(value)=="object"?JSON.stringify(value):value; if (direction.toLowerCase()==="left" || direction.toLowerCase()==="l") return this._redisCo.lpush(key, realvalue); return this._redisCo.rpush(key, realvalue); } ///获取List getlistValue(key,startPosition,endPosition=-1,jsonFormat=true){ if (jsonFormat==true) return this._redisCo.lrange(key,startPosition,endPosition) .then(result=>{ let retObject = []; result.forEach(element => { retObject.push(JSON.parse(element)) }); return Promise.resolve(retObject); }); else return this._redisCo.lrange(key,startPosition,endPosition); } ///获取List 长度 getlistLength(key){ return this._redisCo.llen(key); } /** * 批量执行 * @param {*} param */ batch(param,cb){ return this.redisClient.multi(param).exec(cb); } } module.exports = RedisUtil;