123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 'use strict';
- const Service = require('egg').Service;
- /**
- * [extends redis操作封装]
- * @type {[type]}
- */
- module.exports = class RedisService extends Service {
-
- /**
- * [get 获取存储对象]
- * @author szjcomo
- * @date 2020-11-03
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- async get(key) {
- let that = this;
- that.nameCheck(key);
- let data = await that.app.redis.get(key);
- if(!data) return false;
- data = JSON.parse(data);
- return data;
- }
- /**
- * [set 设置存储对象]
- * @author szjcomo
- * @date 2020-11-03
- * @param {[type]} key [description]
- * @param {[type]} value [description]
- */
- async set(key,value,seconds = 0) {
- let that = this;
- that.nameCheck(key);
- value = JSON.stringify(value);
- if(!seconds){
- return await that.app.redis.set(key, value);
- }else{
- // 设置有效时间
- return await that.app.redis.set(key, value, 'EX', seconds);
- }
- }
- /**
- * [del 删除存储对象]
- * @author szjcomo
- * @date 2020-11-03
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- async del(key) {
- let that = this;
- that.nameCheck(key);
- return that.app.redis.del(key);
- }
- /**
- * [lpush list左铡链表写入]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @param {[type]} value [description]
- * @return {[type]} [description]
- */
- async lpush(name,value) {
- let that = this;
- that.nameCheck(name);
- value = JSON.stringify(value);
- return await that.app.redis.lpush(name,value);
- }
- /**
- * [rpush list右侧链表写入]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @param {[type]} value [description]
- * @return {[type]} [description]
- */
- async rpush(name,value) {
- let that = this;
- that.nameCheck(name);
- value = JSON.stringify(value);
- return await that.app.redis.rpush(name,value);
- }
- /**
- * [lpop list左侧弹出并移除元素]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @return {[type]} [description]
- */
- async lpop(name) {
- let that = this;
- that.nameCheck(name);
- return await that.app.redis.lpop(name);
- }
- /**
- * [rpop list 右侧弹出并移除元素]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @return {[type]} [description]
- */
- async rpop(name) {
- let that = this;
- that.nameCheck(name);
- return await that.app.redis.rpop(name);
- }
- /**
- * [llen 获取list长度]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @return {[type]} [description]
- */
- async llen(name) {
- let that = this;
- that.nameCheck(name);
- return await that.app.redis.llen(name);
- }
- /**
- * [lrange 获取列表指定范围内的元素]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @param {Number} start [description]
- * @param {Number} stop [description]
- * @return {[type]} [description]
- */
- async lrange(name,start = 0,stop = 0) {
- let that = this;
- that.nameCheck(name);
- if(!stop) {
- let length = await that.llen(name);
- stop = (length+1);
- }
- let arr = await that.app.redis.lrange(name,start,stop);
- let result = [];
- arr.forEach(item => {result.push(JSON.parse(item));});
- return result;
- }
- /**
- * [ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @param {Number} start [description]
- * @param {Number} stop [description]
- * @return {[type]} [description]
- */
- async ltrim(name,start = 0,stop = 0) {
- let that = this;
- that.nameCheck(name);
- if(!stop) {
- let length = await that.llen(name);
- stop = (length+1);
- }
- return await that.app.redis.ltrim(name,start,stop);
- }
- /**
- * [nameCheck 检测name]
- * @author szjcomo
- * @date 2020-11-04
- * @param {[type]} name [description]
- * @return {[type]} [description]
- */
- nameCheck(name) {
- if(!name || name.length == 0) throw new Error('redis key不能为空');
- }
- }
|