当前位置:   article > 正文

通用的RedisUtil工具类

redisutil工具类

日常开发工作中,用到缓存Redis,整理了一个工具类,供大家参考使用

工具类如下:

  1. import lombok.RequiredArgsConstructor;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.dao.DataAccessException;
  4. import org.springframework.data.redis.RedisConnectionFailureException;
  5. import org.springframework.data.redis.connection.RedisConnection;
  6. import org.springframework.data.redis.core.*;
  7. import java.util.*;
  8. import java.util.concurrent.TimeUnit;
  9. /**
  10. * @author user
  11. */
  12. @RequiredArgsConstructor
  13. @Slf4j
  14. public class RedisUtils {
  15. public final static int RETRY_CNT = 10;
  16. public final RedisTemplate<String, Object> redisTemplate;
  17. public Boolean hasKey(String key) {
  18. return retryHasKey(key, 0);
  19. }
  20. public void saveObject(String key, Object value) {
  21. retrySaveObject(key, value, 0);
  22. }
  23. public void saveHash(String key, String hashKey, Object value) {
  24. retrySaveHash(key, hashKey, value, 0);
  25. }
  26. public void addSetObject(String key, Object value) {
  27. retrySetAddObject(key, value, 0);
  28. }
  29. public Set<Object> getSetObject(String key) {
  30. return retrySetGetObject(key, 0);
  31. }
  32. public <T> List<T> getMultiObjects(Collection<String> keys) {
  33. return retryMultiGet(keys, 0);
  34. }
  35. public <T> List<T> getHashMultiValues(String key, List<Object> hashkeys) {
  36. return retryGetHashValues(key, hashkeys, 0);
  37. }
  38. public void saveTimeOut(String key, Object value, long timeout, TimeUnit unit) {
  39. retrySaveObjectTimeOut(key, value, timeout, unit, 0);
  40. }
  41. public void saveTimeOut(String key, Object value, int timeout) {
  42. retrySaveObjectTimeOut(key, value, timeout, TimeUnit.MINUTES, 0);
  43. }
  44. public void removeSetObject(String key, Object value) {
  45. retrySetRemoveObject(key, value, 0);
  46. }
  47. public Long getSetSize(String key) {
  48. return retryGetSetSize(key, 0);
  49. }
  50. public Map<Object, Object> getHashEntries(String key) {
  51. return retryGetHashEntries(key, 0);
  52. }
  53. public <T> T getHashValue(String key, Object hashKey) {
  54. return retryGetHashValue(key, hashKey, 0);
  55. }
  56. public Long IncrementHashValue(String key, Object hashKey, long delta) {
  57. return retryIncrementHashValue(key, hashKey, delta, 0);
  58. }
  59. public Set<String> getPatternkeys(String pattern) {
  60. return redisTemplate.execute(new RedisCallback<Set<String>>() {
  61. @Override
  62. public Set<String> doInRedis(RedisConnection connection) throws DataAccessException {
  63. Set<String> keys = new HashSet<>();
  64. ScanOptions.ScanOptionsBuilder scanOptionsBuilder = ScanOptions.scanOptions();
  65. scanOptionsBuilder.match(pattern);
  66. Cursor<byte[]> cursor = connection.scan(scanOptionsBuilder.build());
  67. while (cursor.hasNext()) {
  68. keys.add(new String(cursor.next()));
  69. }
  70. return keys;
  71. }
  72. });
  73. }
  74. private Long retryIncrementHashValue(String key, Object hashKey, long delta, int cnt) {
  75. try {
  76. return redisTemplate.opsForHash().increment(key, hashKey, delta);
  77. } catch (RedisConnectionFailureException ex) {
  78. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  79. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  80. }
  81. if (cnt > RETRY_CNT) {
  82. throw new RuntimeException(
  83. "retryIncrementHashValues(String key, Object hashKey)方法没有成功,key is " + key + " hashKey is " + hashKey);
  84. }
  85. return retryIncrementHashValue(key, hashKey, delta, cnt + 1);
  86. }
  87. private <T> List<T> retryGetHashValues(String key, List<Object> hashKeys, int cnt) {
  88. try {
  89. List<Object> value = redisTemplate.opsForHash().multiGet(key, hashKeys);
  90. return (List<T>) value;
  91. } catch (RedisConnectionFailureException ex) {
  92. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  93. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  94. }
  95. if (cnt > RETRY_CNT) {
  96. throw new RuntimeException(
  97. "retryGetHashValues(String key, List<Object> hashKeys)方法没有成功,key is " + key + " hashKeys is " + hashKeys);
  98. }
  99. return retryGetHashValues(key, hashKeys, cnt + 1);
  100. }
  101. private <T> T retryGetHashValue(String key, Object hashKey, int cnt) {
  102. try {
  103. Object value = redisTemplate.opsForHash().get(key, hashKey);
  104. if (value != null) {
  105. return (T) value;
  106. } else {
  107. return null;
  108. }
  109. } catch (RedisConnectionFailureException ex) {
  110. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  111. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  112. }
  113. if (cnt > RETRY_CNT) {
  114. throw new RuntimeException(
  115. "retryGetHashValue(String key, Object hashKey)方法没有成功,key is " + key + " hashKey is " + hashKey);
  116. }
  117. return retryGetHashValue(key, hashKey, cnt + 1);
  118. }
  119. private Map<Object, Object> retryGetHashEntries(String key, int cnt) {
  120. try {
  121. return redisTemplate.opsForHash().entries(key);
  122. } catch (RedisConnectionFailureException ex) {
  123. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  124. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  125. }
  126. if (cnt > RETRY_CNT) {
  127. throw new RuntimeException("getHashEntries(String key)方法没有成功,key is " + key);
  128. }
  129. return retryGetHashEntries(key, cnt + 1);
  130. }
  131. public <T> T getObject(String key) {
  132. return retryGetObject(key, 0);
  133. }
  134. private <T> T retryGetObject(String key, int cnt) {
  135. try {
  136. Object value = redisTemplate.opsForValue().get(key);
  137. if (value != null) {
  138. return (T) value;
  139. } else {
  140. return null;
  141. }
  142. } catch (RedisConnectionFailureException ex) {
  143. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  144. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  145. }
  146. if (cnt > RETRY_CNT) {
  147. throw new RuntimeException("getObjByString(String key)方法没有成功,key is " + key);
  148. }
  149. return retryGetObject(key, cnt + 1);
  150. }
  151. private <T> List<T> retryMultiGet(Collection<String> keys, int cnt) {
  152. try {
  153. List<Object> value = redisTemplate.opsForValue().multiGet(keys);
  154. if (value != null) {
  155. return (List<T>) value;
  156. } else {
  157. return null;
  158. }
  159. } catch (RedisConnectionFailureException ex) {
  160. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  161. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  162. }
  163. if (cnt > RETRY_CNT) {
  164. throw new RuntimeException("retryMultiGet(String keys)方法没有成功,keys is " + keys);
  165. }
  166. return retryMultiGet(keys, cnt + 1);
  167. }
  168. private void retrySaveObject(String key, Object value, int cnt) {
  169. try {
  170. redisTemplate.opsForValue().set(key, value);
  171. return;
  172. } catch (RedisConnectionFailureException ex) {
  173. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  174. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  175. }
  176. if (cnt > RETRY_CNT) {
  177. throw new RuntimeException("retrySaveObject(String key, String value)方法没有成功,key is " + key + " value is " + value);
  178. }
  179. retrySaveObject(key, value, cnt + 1);
  180. }
  181. private void retrySaveObjectTimeOut(String key, Object value, long timeout, TimeUnit unit, int cnt) {
  182. try {
  183. redisTemplate.opsForValue().set(key, value, timeout, unit);
  184. return;
  185. } catch (RedisConnectionFailureException ex) {
  186. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  187. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  188. }
  189. if (cnt > RETRY_CNT) {
  190. throw new RuntimeException(
  191. "retrySaveObjectTimeOut(String key, String value)方法没有成功,key is " + key + " value is " + value);
  192. }
  193. retrySaveObjectTimeOut(key, value, timeout, unit, cnt + 1);
  194. }
  195. private void retrySetAddObject(String key, Object value, int cnt) {
  196. try {
  197. redisTemplate.opsForSet().add(key, value);
  198. return;
  199. } catch (RedisConnectionFailureException ex) {
  200. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  201. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  202. }
  203. if (cnt > RETRY_CNT) {
  204. throw new RuntimeException("retrySetAdd(String key, String value)方法没有成功,key is " + key + " value is " + value);
  205. }
  206. retrySetAddObject(key, value, cnt + 1);
  207. }
  208. private Set<Object> retrySetGetObject(String key, int cnt) {
  209. try {
  210. return redisTemplate.opsForSet().members(key);
  211. } catch (RedisConnectionFailureException ex) {
  212. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  213. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  214. }
  215. if (cnt > RETRY_CNT) {
  216. throw new RuntimeException("retrySetGetObject(String key)方法没有成功,key is " + key);
  217. }
  218. return retrySetGetObject(key, cnt + 1);
  219. }
  220. private void retrySetRemoveObject(String key, Object value, int cnt) {
  221. try {
  222. redisTemplate.opsForSet().remove(key, value);
  223. return;
  224. } catch (RedisConnectionFailureException ex) {
  225. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  226. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  227. }
  228. if (cnt > RETRY_CNT) {
  229. throw new RuntimeException(
  230. "retrySetRemoveObject(String key, String value)方法没有成功,key is " + key + " value is " + value);
  231. }
  232. retrySetRemoveObject(key, value, cnt + 1);
  233. }
  234. private Long retryGetSetSize(String key, int cnt) {
  235. try {
  236. return redisTemplate.opsForSet().size(key);
  237. } catch (RedisConnectionFailureException ex) {
  238. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  239. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  240. }
  241. if (cnt > RETRY_CNT) {
  242. throw new RuntimeException("retryGetSetSize(String key, String value)方法没有成功,key is " + key);
  243. }
  244. return retryGetSetSize(key, cnt + 1);
  245. }
  246. private void retrySaveHash(String key, String hashKey, Object value, int cnt) {
  247. try {
  248. redisTemplate.opsForHash().put(key, hashKey, value);
  249. return;
  250. } catch (RedisConnectionFailureException ex) {
  251. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  252. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  253. }
  254. if (cnt <= RETRY_CNT) {
  255. throw new RuntimeException("saveHash(String key, String hashKey, String value)方法没有成功,key is " + key + ",field is "
  256. + hashKey + ", value is " + value);
  257. }
  258. retrySaveHash(key, hashKey, value, cnt + 1);
  259. }
  260. private Boolean retryHasKey(String key, int cnt) {
  261. try {
  262. return redisTemplate.hasKey(key);
  263. } catch (RedisConnectionFailureException ex) {
  264. RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  265. log.error("redis 操作异常,当前第" + cnt + "次", ex);
  266. }
  267. if (cnt > RETRY_CNT) {
  268. throw new RuntimeException("hasKey(String key) 方法没有成功,key is " + key);
  269. }
  270. return retryHasKey(key, cnt + 1);
  271. }
  272. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/240546
推荐阅读
相关标签
  

闽ICP备14008679号