RedisClient.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package com.example.demo.common.utils;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.stereotype.Component;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.concurrent.TimeUnit;
  8. @Component
  9. public class RedisClient {
  10. @Autowired
  11. private RedisTemplate<String, Object> redisTemplate;
  12. /**
  13. * 指定缓存失效时间
  14. * @param key 键
  15. * @param time 时间(秒)
  16. * @return
  17. */
  18. public boolean expire(String key,long time){
  19. try {
  20. if(time>0){
  21. redisTemplate.expire(key, time, TimeUnit.SECONDS);
  22. }
  23. return true;
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. return false;
  27. }
  28. }
  29. /**
  30. * 根据key 获取过期时间
  31. * @param key 键 不能为null
  32. * @return 时间(秒) 返回0代表为永久有效
  33. */
  34. public long ttl(String key){
  35. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  36. }
  37. /**
  38. * 判断key是否存在
  39. * @param key 键
  40. * @return true 存在 false不存在
  41. */
  42. public Boolean exists(String key){
  43. return redisTemplate.hasKey(key);
  44. }
  45. //============================String=============================
  46. /**
  47. * 普通缓存获取
  48. * @param key 键
  49. * @return 值
  50. */
  51. public Object get(String key){
  52. return key==null?null:redisTemplate.opsForValue().get(key);
  53. }
  54. /**
  55. * 普通缓存放入
  56. * @param key 键
  57. * @param value 值
  58. * @return true成功 false失败
  59. */
  60. public boolean set(String key,Object value) {
  61. try {
  62. redisTemplate.opsForValue().set(key, value);
  63. return true;
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. return false;
  67. }
  68. }
  69. /**
  70. * 删除缓存
  71. * @param key 可以传一个值 或多个
  72. */
  73. public Boolean del(String key){
  74. return redisTemplate.delete(key);
  75. }
  76. /**
  77. * 递增
  78. * @param key 键
  79. * @param delta 要增加几(大于0)
  80. * @return
  81. */
  82. public long incr(String key, long delta){
  83. if(delta<0){
  84. throw new RuntimeException("递增因子必须大于0");
  85. }
  86. return redisTemplate.opsForValue().increment(key, delta);
  87. }
  88. /**
  89. * 递减
  90. * @param key 键
  91. * @param delta 要减少几(小于0)
  92. * @return
  93. */
  94. public long decr(String key, long delta){
  95. if(delta<0){
  96. throw new RuntimeException("递减因子必须大于0");
  97. }
  98. return redisTemplate.opsForValue().decrement(key, -delta);
  99. }
  100. //================================hash=================================
  101. /**
  102. * HashGet
  103. * @param key 键 不能为null
  104. * @param item 项 不能为null
  105. * @return 值
  106. */
  107. public Object hget(String key,String item){
  108. return redisTemplate.opsForHash().get(key, item);
  109. }
  110. /**
  111. * 向一张hash表中放入数据,如果不存在将创建
  112. * @param key 键
  113. * @param item 项
  114. * @param value 值
  115. * @return true 成功 false失败
  116. */
  117. public boolean hset(String key,String item,Object value) {
  118. try {
  119. redisTemplate.opsForHash().put(key, item, value);
  120. return true;
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. return false;
  124. }
  125. }
  126. /**
  127. * 删除hash表中的值
  128. * @param key 键 不能为null
  129. * @param item 项 可以使多个 不能为null
  130. */
  131. public void hdel(String key, Object... item){
  132. redisTemplate.opsForHash().delete(key,item);
  133. }
  134. //============================set=============================
  135. /**
  136. * 根据key获取Set中的所有值
  137. * @param key 键
  138. * @return
  139. */
  140. public Set<Object> smembers(String key){
  141. try {
  142. return redisTemplate.opsForSet().members(key);
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. return null;
  146. }
  147. }
  148. /**
  149. * 将数据放入set缓存
  150. * @param key 键
  151. * @param values 值 可以是多个
  152. * @return 成功个数
  153. */
  154. public long sadd(String key, Object...values) {
  155. try {
  156. return redisTemplate.opsForSet().add(key, values);
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. return 0;
  160. }
  161. }
  162. /**
  163. * 移除值为value的
  164. * @param key 键
  165. * @param values 值 可以是多个
  166. * @return 移除的个数
  167. */
  168. public long srem(String key, Object ...values) {
  169. try {
  170. Long count = redisTemplate.opsForSet().remove(key, values);
  171. return count;
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. return 0;
  175. }
  176. }
  177. /**
  178. * 求差集
  179. * @param key1
  180. * @param key2
  181. * @return
  182. */
  183. public Set<Object> diff(String key1, String key2) {
  184. try {
  185. return redisTemplate.opsForSet().difference(key1, key2);
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. return null;
  189. }
  190. }
  191. //===============================list=================================
  192. /**
  193. * 获取list缓存的内容
  194. * @param key 键
  195. * @param start 开始
  196. * @param end 结束 0 到 -1代表所有值
  197. * @return
  198. */
  199. public List<Object> lrange(String key, long start, long end){
  200. try {
  201. return redisTemplate.opsForList().range(key, start, end);
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. return null;
  205. }
  206. }
  207. /**
  208. * 将list放入缓存
  209. * @param key 键
  210. * @param value 值
  211. * @return
  212. */
  213. public boolean rpush(String key, Object value) {
  214. try {
  215. redisTemplate.opsForList().rightPush(key, value);
  216. return true;
  217. } catch (Exception e) {
  218. e.printStackTrace();
  219. return false;
  220. }
  221. }
  222. /**
  223. * 将list放入缓存
  224. * @param key 键
  225. * @param value 值
  226. * @return
  227. */
  228. public boolean lpush(String key, List<Object> value) {
  229. try {
  230. redisTemplate.opsForList().rightPushAll(key, value);
  231. return true;
  232. } catch (Exception e) {
  233. e.printStackTrace();
  234. return false;
  235. }
  236. }
  237. public Set<Object> sdiff(String keya,String keyb){
  238. try {
  239. return redisTemplate.opsForSet().difference(keya,keyb);
  240. } catch (Exception e) {
  241. e.printStackTrace();
  242. return null;
  243. }
  244. }
  245. /**
  246. * 移除N个值为value
  247. * @param key 键
  248. * @param count 移除多少个
  249. * @param value 值
  250. * @return 移除的个数
  251. */
  252. public long lrem(String key,long count,Object value) {
  253. try {
  254. Long remove = redisTemplate.opsForList().remove(key, count, value);
  255. return remove;
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. return 0;
  259. }
  260. }
  261. }