redis.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. /**
  4. * [extends redis操作封装]
  5. * @type {[type]}
  6. */
  7. module.exports = class RedisService extends Service {
  8. /**
  9. * [get 获取存储对象]
  10. * @author szjcomo
  11. * @date 2020-11-03
  12. * @param {[type]} key [description]
  13. * @return {[type]} [description]
  14. */
  15. async get(key) {
  16. let that = this;
  17. that.nameCheck(key);
  18. let data = await that.app.redis.get(key);
  19. if(!data) return false;
  20. data = JSON.parse(data);
  21. return data;
  22. }
  23. /**
  24. * [set 设置存储对象]
  25. * @author szjcomo
  26. * @date 2020-11-03
  27. * @param {[type]} key [description]
  28. * @param {[type]} value [description]
  29. */
  30. async set(key,value,seconds = 0) {
  31. let that = this;
  32. that.nameCheck(key);
  33. value = JSON.stringify(value);
  34. if(!seconds){
  35. return await that.app.redis.set(key, value);
  36. }else{
  37. // 设置有效时间
  38. return await that.app.redis.set(key, value, 'EX', seconds);
  39. }
  40. }
  41. /**
  42. * [del 删除存储对象]
  43. * @author szjcomo
  44. * @date 2020-11-03
  45. * @param {[type]} key [description]
  46. * @return {[type]} [description]
  47. */
  48. async del(key) {
  49. let that = this;
  50. that.nameCheck(key);
  51. return that.app.redis.del(key);
  52. }
  53. /**
  54. * [lpush list左铡链表写入]
  55. * @author szjcomo
  56. * @date 2020-11-04
  57. * @param {[type]} name [description]
  58. * @param {[type]} value [description]
  59. * @return {[type]} [description]
  60. */
  61. async lpush(name,value) {
  62. let that = this;
  63. that.nameCheck(name);
  64. value = JSON.stringify(value);
  65. return await that.app.redis.lpush(name,value);
  66. }
  67. /**
  68. * [rpush list右侧链表写入]
  69. * @author szjcomo
  70. * @date 2020-11-04
  71. * @param {[type]} name [description]
  72. * @param {[type]} value [description]
  73. * @return {[type]} [description]
  74. */
  75. async rpush(name,value) {
  76. let that = this;
  77. that.nameCheck(name);
  78. value = JSON.stringify(value);
  79. return await that.app.redis.rpush(name,value);
  80. }
  81. /**
  82. * [lpop list左侧弹出并移除元素]
  83. * @author szjcomo
  84. * @date 2020-11-04
  85. * @param {[type]} name [description]
  86. * @return {[type]} [description]
  87. */
  88. async lpop(name) {
  89. let that = this;
  90. that.nameCheck(name);
  91. return await that.app.redis.lpop(name);
  92. }
  93. /**
  94. * [rpop list 右侧弹出并移除元素]
  95. * @author szjcomo
  96. * @date 2020-11-04
  97. * @param {[type]} name [description]
  98. * @return {[type]} [description]
  99. */
  100. async rpop(name) {
  101. let that = this;
  102. that.nameCheck(name);
  103. return await that.app.redis.rpop(name);
  104. }
  105. /**
  106. * [llen 获取list长度]
  107. * @author szjcomo
  108. * @date 2020-11-04
  109. * @param {[type]} name [description]
  110. * @return {[type]} [description]
  111. */
  112. async llen(name) {
  113. let that = this;
  114. that.nameCheck(name);
  115. return await that.app.redis.llen(name);
  116. }
  117. /**
  118. * [lrange 获取列表指定范围内的元素]
  119. * @author szjcomo
  120. * @date 2020-11-04
  121. * @param {[type]} name [description]
  122. * @param {Number} start [description]
  123. * @param {Number} stop [description]
  124. * @return {[type]} [description]
  125. */
  126. async lrange(name,start = 0,stop = 0) {
  127. let that = this;
  128. that.nameCheck(name);
  129. if(!stop) {
  130. let length = await that.llen(name);
  131. stop = (length+1);
  132. }
  133. let arr = await that.app.redis.lrange(name,start,stop);
  134. let result = [];
  135. arr.forEach(item => {result.push(JSON.parse(item));});
  136. return result;
  137. }
  138. /**
  139. * [ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。]
  140. * @author szjcomo
  141. * @date 2020-11-04
  142. * @param {[type]} name [description]
  143. * @param {Number} start [description]
  144. * @param {Number} stop [description]
  145. * @return {[type]} [description]
  146. */
  147. async ltrim(name,start = 0,stop = 0) {
  148. let that = this;
  149. that.nameCheck(name);
  150. if(!stop) {
  151. let length = await that.llen(name);
  152. stop = (length+1);
  153. }
  154. return await that.app.redis.ltrim(name,start,stop);
  155. }
  156. /**
  157. * [nameCheck 检测name]
  158. * @author szjcomo
  159. * @date 2020-11-04
  160. * @param {[type]} name [description]
  161. * @return {[type]} [description]
  162. */
  163. nameCheck(name) {
  164. if(!name || name.length == 0) throw new Error('redis key不能为空');
  165. }
  166. }