RedisUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package top.lvzhiqiang.util;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.concurrent.TimeUnit;
  6. /**
  7. * Redis工具类
  8. *
  9. * @author 施勇
  10. * 2018年11月27日 下午9:26:11
  11. */
  12. public class RedisUtils {
  13. private RedisTemplate<String, Object> redisTemplate;
  14. public RedisTemplate<String, Object> getRedisTemplate() {
  15. return redisTemplate;
  16. }
  17. public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
  18. this.redisTemplate = redisTemplate;
  19. }
  20. /**
  21. * 缓存对象信息
  22. *
  23. * @param key 键
  24. * @param value 值
  25. * @author 施勇
  26. * 2018年12月11日 上午10:54:49
  27. */
  28. public void set(String key, Object value) {
  29. redisTemplate.opsForValue().set(key, value);
  30. }
  31. /**
  32. * 缓存信息,并设置缓存时间
  33. *
  34. * @param key 键
  35. * @param value 值
  36. * @param time 时间
  37. * @param timeUnit 时间单位
  38. * @author 施勇
  39. * 2018年12月11日 上午10:54:27
  40. */
  41. public void set(String key, Object value, long time, TimeUnit timeUnit) {
  42. if (time > 0) {
  43. redisTemplate.opsForValue().set(key, value, time, timeUnit);
  44. } else {
  45. set(key, value);
  46. }
  47. }
  48. /**
  49. * 原子递增1
  50. *
  51. * @param key 键
  52. * @return java.lang.Long
  53. */
  54. public Long increment(String key) {
  55. return redisTemplate.opsForValue().increment(key);
  56. }
  57. /**
  58. * 原子递增
  59. *
  60. * @param key 键
  61. * @param delta 增量
  62. * @return java.lang.Long
  63. */
  64. public Long increment(String key, Long delta) {
  65. if (delta == null || delta < 1) {
  66. delta = 1L;
  67. }
  68. return redisTemplate.opsForValue().increment(key, delta);
  69. }
  70. /**
  71. * 递增
  72. *
  73. * @param key 键
  74. * @param delta 递增因子
  75. * @param time 时间
  76. * @param timeUnit 时间单位
  77. * @return java.lang.Long
  78. * @author shiyong
  79. * 2022/10/18 14:58
  80. */
  81. public Long increment(String key, Long delta, long time, TimeUnit timeUnit) {
  82. if (null == delta || delta < 1) {
  83. delta = 1L;
  84. }
  85. Long increment = redisTemplate.opsForValue().increment(key, delta);
  86. redisTemplate.expire(key, time, timeUnit);
  87. return increment;
  88. }
  89. /**
  90. * 从缓存中获取信息
  91. *
  92. * @param key 键
  93. * @return Object
  94. * @author 施勇
  95. * 2018年12月11日 上午10:54:10
  96. */
  97. public Object get(Object key) {
  98. return key == null ? "" : redisTemplate.opsForValue().get(key);
  99. }
  100. /**
  101. * 从redis中获取key对应的过期时间;
  102. * 如果该值有过期时间,就返回相应的过期时间;
  103. * 如果该值没有设置过期时间,就返回-1;
  104. * 如果没有该值,就返回-2;
  105. */
  106. public Long getExpire(String key) {
  107. return redisTemplate.opsForValue().getOperations().getExpire(key);
  108. }
  109. /**
  110. * 设置key的失效时间
  111. *
  112. * @param key 键
  113. * @param time 时长
  114. * @param timeUnit 时间单位
  115. * @return java.lang.Boolean
  116. */
  117. public Boolean expire(String key, long time, TimeUnit timeUnit) {
  118. return redisTemplate.expire(key, time, timeUnit);
  119. }
  120. /**
  121. * 是否存在key
  122. *
  123. * @param key 键
  124. * @return boolean
  125. * @author 施勇
  126. * 2018年12月11日 上午10:53:54
  127. */
  128. public boolean hasKey(String key) {
  129. if (null == key || "".equals(key)) {
  130. return false;
  131. }
  132. Boolean result = redisTemplate.hasKey(key);
  133. if (null == result) {
  134. return false;
  135. } else {
  136. return result;
  137. }
  138. }
  139. /**
  140. * 删除KEY对应的对象
  141. *
  142. * @param key 键
  143. * @author 施勇
  144. * 2018年12月24日 下午4:14:26
  145. */
  146. public void delete(String key) {
  147. redisTemplate.delete(key);
  148. }
  149. /**
  150. * 查找匹配的key
  151. *
  152. * @param pattern 匹配字符串
  153. * @return java.util.Set<java.lang.String>
  154. * @author shiyong
  155. * 2020/1/3 13:42
  156. */
  157. public Set<String> getKeys(String pattern) {
  158. return redisTemplate.keys(pattern);
  159. }
  160. /**
  161. * 在队尾追加元素
  162. *
  163. * @param key 键
  164. * @param value 值
  165. * @return 添加后队列的长度
  166. */
  167. public Long rightPush(String key, Object value) {
  168. return redisTemplate.opsForList().rightPush(key, value);
  169. }
  170. /**
  171. * 从队首移除元素
  172. *
  173. * @param key 键
  174. * @return 移除的元素
  175. */
  176. public Object leftPop(String key) {
  177. return redisTemplate.opsForList().leftPop(key);
  178. }
  179. /**
  180. * 从队首开始取值,获取指定区间
  181. *
  182. * @param key 键
  183. * @param start 开始下标
  184. * @param stop 结束下标
  185. * @return 区间内的全部值
  186. */
  187. public List<?> leftRange(String key, long start, long stop) {
  188. return redisTemplate.opsForList().range(key, start, stop);
  189. }
  190. /**
  191. * 将值加到集合中
  192. *
  193. * @param key 键
  194. * @param values 值
  195. * @return 添加是否成功(1:成功,0:失败)
  196. */
  197. public Long addToSet(String key, Object... values) {
  198. return redisTemplate.opsForSet().add(key, values);
  199. }
  200. /**
  201. * 集合的数量
  202. *
  203. * @param key 键
  204. * @return 集合数量
  205. */
  206. public Long sizeOfSet(String key) {
  207. return redisTemplate.opsForSet().size(key);
  208. }
  209. /**
  210. * 集合列表
  211. *
  212. * @param key 键
  213. * @return 集合列表
  214. */
  215. public Set<Object> getOfSet(String key) {
  216. return redisTemplate.opsForSet().members(key);
  217. }
  218. /**
  219. * 集合中是否有该值
  220. *
  221. * @param key 键
  222. * @param value 值
  223. * @return Boolean
  224. */
  225. public Boolean hasOfSet(String key, Object value) {
  226. return redisTemplate.opsForSet().isMember(key, value);
  227. }
  228. /**
  229. * 删除集合中值
  230. *
  231. * @param key 键
  232. * @param value 值
  233. * @return 删除是否成功(1:成功,0:失败)
  234. */
  235. public Long removeOfSet(String key, Object value) {
  236. return redisTemplate.opsForSet().remove(key, value);
  237. }
  238. }