RedisUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.kcim.common.token;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.HashOperations;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.data.redis.support.atomic.RedisAtomicLong;
  6. import org.springframework.stereotype.Component;
  7. import java.time.Duration;
  8. import java.util.Map;
  9. import java.util.concurrent.TimeUnit;
  10. /**
  11. * Redis的工具类文件, 注意结果可能不是你想要的,建议使用string
  12. */
  13. @Component
  14. public class RedisUtil {
  15. @Autowired
  16. private RedisTemplate redisTemplate;
  17. @Autowired
  18. private HashOperations hashOperations;
  19. /**
  20. * 指定缓存失效时间
  21. *
  22. * @param key 键
  23. * @param time 时间(秒)
  24. * @return
  25. */
  26. public boolean expire(String key, long time) {
  27. try {
  28. if (time > 0) {
  29. redisTemplate.expire(key, time, TimeUnit.SECONDS);
  30. }
  31. return true;
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. return false;
  35. }
  36. }
  37. /**
  38. * 根据key 获取过期时间
  39. *
  40. * @param key 键 不能为null
  41. * @return 时间(秒) 返回0代表为永久有效
  42. */
  43. public long getExpire(String key) {
  44. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  45. }
  46. /**
  47. * 判断key是否存在
  48. *
  49. * @param key 键
  50. * @return true 存在 false不存在
  51. */
  52. public boolean hasKey(String key) {
  53. try {
  54. return redisTemplate.hasKey(key);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. return false;
  58. }
  59. }
  60. /**
  61. * 普通缓存获取
  62. *
  63. * @param key 键
  64. * @return 值
  65. */
  66. public Object get(String key) {
  67. return key == null ? null : redisTemplate.opsForValue().get(key);
  68. }
  69. /**
  70. * 删除缓存
  71. *
  72. * @param key 可以传一个值
  73. */
  74. @SuppressWarnings("unchecked")
  75. public void del(Object key) {
  76. redisTemplate.delete(key);
  77. }
  78. /**
  79. * 普通缓存放入
  80. *
  81. * @param key 键
  82. * @param value 值
  83. * @return true成功 false失败
  84. */
  85. public boolean set(String key, Object value) {
  86. try {
  87. redisTemplate.opsForValue().set(key, value);
  88. return true;
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. return false;
  92. }
  93. }
  94. /**
  95. * 普通缓存放入并设置时间
  96. *
  97. * @param key
  98. * @param value
  99. * @param duration 过期时间,灵活选择
  100. * @return
  101. */
  102. public boolean set(String key, Object value, Duration duration) {
  103. try {
  104. redisTemplate.opsForValue().set(key, value, duration);
  105. return true;
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. return false;
  109. }
  110. }
  111. /**
  112. * 普通缓存放入并设置时间
  113. *
  114. * @param key 键
  115. * @param value 值
  116. * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
  117. * @return true成功 false 失败
  118. */
  119. public boolean set(String key, Object value, long time) {
  120. try {
  121. if (time > 0) {
  122. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  123. } else {
  124. set(key, value);
  125. }
  126. return true;
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. return false;
  130. }
  131. }
  132. /**
  133. * Push到 队列List
  134. *
  135. * @param key
  136. * @param value
  137. * @param
  138. */
  139. public void leftPush(String key, Object value) {
  140. redisTemplate.opsForList().leftPush(key, value);
  141. }
  142. /**
  143. * Push到 队列List
  144. *
  145. * @param key
  146. * @param value
  147. * @param
  148. */
  149. public void leftPush(String key, Object value, long expireTime) {
  150. redisTemplate.opsForList().leftPush(key, value);
  151. expire(key, expireTime);
  152. }
  153. /**
  154. * 从队列List中Pop出来对象
  155. *
  156. * @param key
  157. * @return
  158. */
  159. public Object rightPop(String key) {
  160. return redisTemplate.opsForList().rightPop(key);
  161. }
  162. /**
  163. * 从队列List中Pop出来对象
  164. *
  165. * @param key
  166. * @param t
  167. * @param <T>
  168. * @return
  169. */
  170. public <T> T rightPop(String key, Class<T> t) {
  171. return (T) redisTemplate.opsForList().rightPop(key);
  172. }
  173. /**
  174. * 对缓存在key中的值做减1操作
  175. *
  176. * @param key
  177. */
  178. public Long decrement(String key) {
  179. return redisTemplate.opsForValue().increment(key, -1);
  180. }
  181. /**
  182. * 不存在时设置, 返回true为设置成功
  183. *
  184. * @param key
  185. * @param value
  186. */
  187. public boolean setIfAbsent(Object key, Object value) {
  188. return redisTemplate.opsForValue().setIfAbsent(key, value);
  189. }
  190. /**
  191. * 不存在时设置,同时设置过期时间 返回true为设置成功
  192. *
  193. * @param key
  194. * @param value
  195. * @param timeout
  196. * @return
  197. */
  198. public boolean setIfAbsent(String key, Object value, Duration timeout) {
  199. return redisTemplate.opsForValue().setIfAbsent(key, value, timeout);
  200. }
  201. public long incrementAndGet(String key) {
  202. RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
  203. return entityIdCounter.incrementAndGet();
  204. }
  205. /**
  206. * 存hash值
  207. *
  208. * @param key
  209. * @param hKey
  210. * @param hValue
  211. */
  212. public void hPut(Object key, Object hKey, Object hValue) {
  213. hashOperations.put(key, hKey, hValue);
  214. }
  215. /**
  216. * 存hash值
  217. *
  218. * @param key
  219. * @param map
  220. */
  221. public void hPutAll(Object key, Map map) {
  222. hashOperations.putAll(key, map);
  223. }
  224. /**
  225. * 查hash值
  226. *
  227. * @param key
  228. * @return
  229. */
  230. public Map hEntries(Object key) {
  231. return hashOperations.entries(key);
  232. }
  233. /**
  234. * 查hash值
  235. *
  236. * @param key
  237. * @param hKey
  238. * @return
  239. */
  240. public Object hGet(Object key, Object hKey) {
  241. return hashOperations.get(key, hKey);
  242. }
  243. }