RedisService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package com.caimei.components;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.data.redis.core.ValueOperations;
  6. import org.springframework.stereotype.Service;
  7. import javax.annotation.Resource;
  8. import java.io.Serializable;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.concurrent.TimeUnit;
  13. /**
  14. * Redis 服务工具类
  15. *
  16. * @author : Charles
  17. * @date : 2021/3/4
  18. */
  19. @Slf4j
  20. @Service
  21. public class RedisService {
  22. @Resource
  23. private RedisTemplate<Serializable, Object> redisTemplate;
  24. /**
  25. * 批量删除
  26. * @param keys
  27. */
  28. public void remove(String... keys) {
  29. for(String key :keys){
  30. remove(key);
  31. }
  32. }
  33. /**
  34. * 批量删除正则匹配到的
  35. * @param pattern
  36. */
  37. public void removePattern(String pattern) {
  38. Set<Serializable> keys = redisTemplate.keys(pattern);
  39. assert keys != null;
  40. if (keys.size() > 0){
  41. redisTemplate.delete(keys);
  42. }
  43. }
  44. /**
  45. * 删除
  46. * @param key
  47. */
  48. public void remove(String key) {
  49. if (exists(key)) {
  50. redisTemplate.delete(key);
  51. }
  52. }
  53. /**
  54. * 判断缓存中是否存在
  55. * @param key
  56. * @return boolean
  57. */
  58. public boolean exists(String key) {
  59. return StringUtils.isBlank(key) ? false : redisTemplate.hasKey(key);
  60. }
  61. /**
  62. * 读取缓存
  63. * @param key
  64. * @return
  65. */
  66. public Object get(String key) {
  67. Object result = null;
  68. ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
  69. result = operations.get(key);
  70. return result;
  71. }
  72. /**
  73. * 写入缓存
  74. * @param key
  75. * @param value
  76. * @return
  77. */
  78. public boolean set(String key, Object value) {
  79. boolean result = false;
  80. try {
  81. ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
  82. operations.set(key, value);
  83. result = true;
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. return result;
  88. }
  89. /**
  90. * 写入缓存并加上过期时间(秒)
  91. * @param key
  92. * @param value
  93. * @param expireTimeSeconds
  94. * @return
  95. */
  96. public boolean set(String key, Object value, Long expireTimeSeconds) {
  97. boolean result = false;
  98. try {
  99. ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
  100. operations.set(key, value);
  101. redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
  102. result = true;
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return result;
  107. }
  108. /**
  109. * 写入过期时间(秒)
  110. * @param key
  111. * @param expireTimeSeconds
  112. * @return
  113. */
  114. public boolean expire(String key, Long expireTimeSeconds) {
  115. boolean result = false;
  116. try {
  117. redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
  118. result = true;
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. return result;
  123. }
  124. /* **************************** 针对list操作的方法 **************************** */
  125. /**
  126. * 在key对应list的尾部添加
  127. * @param key
  128. * @param value
  129. * @return
  130. */
  131. public long rightPushForList(String key, Object value) {
  132. return redisTemplate.opsForList().rightPush(key, value);
  133. }
  134. /**
  135. * 在key对应list的头部添加
  136. * @param key
  137. * @param value
  138. * @return
  139. */
  140. public long leftPushForList(String key, Object value) {
  141. return redisTemplate.opsForList().leftPush(key, value);
  142. }
  143. /**
  144. * key对应list的长度
  145. * @param key
  146. * @return
  147. */
  148. public long listSize(String key) {
  149. return redisTemplate.opsForList().size(key);
  150. }
  151. /**
  152. * 获取list集合
  153. * @param Key
  154. * @param begin
  155. * @param end
  156. * @return
  157. */
  158. public List<?> getList(String Key, int begin, int end) {
  159. return redisTemplate.opsForList().range(Key, begin, end);
  160. }
  161. /**
  162. * 在key对应list的尾部移除
  163. * @param key
  164. * @return
  165. */
  166. public Object rightPopForList(String key) {
  167. return redisTemplate.opsForList().rightPop(key);
  168. }
  169. /**
  170. * 在key对应list的头部移除
  171. * @param key
  172. * @return
  173. */
  174. public Object leftPopForList(String key) {
  175. return redisTemplate.opsForList().leftPop(key);
  176. }
  177. /**
  178. * 移除list的index位置上的值
  179. * @param Key
  180. * @param index
  181. * @param value
  182. */
  183. public void removeList(String Key, long index, Object value) {
  184. redisTemplate.opsForList().remove(Key, index, value);
  185. }
  186. /**
  187. * 重置list的index位置上的值
  188. * @param Key
  189. * @param index
  190. * @param value
  191. */
  192. public void setList(String Key, long index, Object value) {
  193. redisTemplate.opsForList().set(Key, index, value);
  194. }
  195. /**
  196. * 写入list
  197. * @param key
  198. * @param list
  199. */
  200. public void setList(String key, List list) {
  201. if(list!=null&&list.size()>0){
  202. for (Object object : list) {
  203. rightPushForList(key,object);
  204. }
  205. }
  206. }
  207. /**
  208. * 写入map
  209. * @param key
  210. * @param map
  211. */
  212. public void setMap(String key, Map<String, Object> map) {
  213. redisTemplate.opsForHash().putAll(key, map);
  214. }
  215. /**
  216. * 获取map
  217. * @param key
  218. * @param mapKey
  219. * @return
  220. */
  221. public Object get(String key, String mapKey) {
  222. return redisTemplate.opsForHash().get(key, mapKey);
  223. }
  224. /**
  225. * 写入map
  226. * @param key
  227. * @param hashKey
  228. * @param hashValue
  229. */
  230. public void setMapByKV(String key, Object hashKey, Object hashValue) {
  231. redisTemplate.opsForHash().put(key, hashKey,hashValue);
  232. }
  233. /**
  234. * 删除map中的某个key-value
  235. * @param key
  236. * @param hashKey
  237. */
  238. public void removeHash(String key, String hashKey) {
  239. redisTemplate.opsForHash().delete(key, hashKey);
  240. }
  241. /**
  242. *
  243. * @param key
  244. * @return
  245. */
  246. public Map<Object, Object> getEntries(String key) {
  247. return redisTemplate.opsForHash().entries(key);
  248. }
  249. /**
  250. *
  251. * @param key
  252. * @param step
  253. * @return
  254. */
  255. public long increase(String key, long step) {
  256. return redisTemplate.opsForValue().increment(key, step);
  257. }
  258. /**
  259. * 获取失效时间
  260. * @param key
  261. * @return
  262. */
  263. public long getExpireTime(String key) {
  264. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  265. }
  266. }