RedisUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.xjrsoft.common.utils;
  2. import cn.hutool.core.lang.TypeReference;
  3. import cn.hutool.json.JSONUtil;
  4. import com.google.gson.Gson;
  5. import com.xjrsoft.common.exception.MyException;
  6. import lombok.AllArgsConstructor;
  7. import org.springframework.data.redis.core.*;
  8. import org.springframework.stereotype.Component;
  9. import java.util.Objects;
  10. import java.util.Set;
  11. import java.util.concurrent.TimeUnit;
  12. import java.util.regex.Pattern;
  13. /**
  14. * @author tzx
  15. * @description reidis操作工具类
  16. * @date 2022-03-02
  17. **/
  18. @Component
  19. @AllArgsConstructor
  20. public class RedisUtil {
  21. private RedisTemplate<String, Object> redisTemplate;
  22. private ValueOperations<String, String> valueOperations;
  23. private HashOperations<String, String, Object> hashOperations;
  24. private ListOperations<String, Object> listOperations;
  25. private SetOperations<String, Object> setOperations;
  26. private ZSetOperations<String, Object> zSetOperations;
  27. /**
  28. * 默认过期时长,单位:秒
  29. */
  30. public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
  31. /**
  32. * 不设置过期时长
  33. */
  34. public final static long NOT_EXPIRE = -1;
  35. private final static Gson GSON = new Gson();
  36. public void set(String key, Object value, long expire) {
  37. valueOperations.set(key, toJson(value));
  38. if (expire != NOT_EXPIRE) {
  39. redisTemplate.expire(key, expire, TimeUnit.SECONDS);
  40. }
  41. }
  42. public void set(String key, Object value) {
  43. set(key, value, NOT_EXPIRE);
  44. }
  45. public <T> T get(String key, Class<T> clazz, long expire) {
  46. String value = valueOperations.get(key);
  47. if (expire != NOT_EXPIRE) {
  48. redisTemplate.expire(key, expire, TimeUnit.SECONDS);
  49. }
  50. return value == null ? null : fromJson(value, clazz);
  51. }
  52. public <T> T get(String key, TypeReference<T> typeReference, long expire) {
  53. String value = valueOperations.get(key);
  54. if (expire != NOT_EXPIRE) {
  55. redisTemplate.expire(key, expire, TimeUnit.SECONDS);
  56. }
  57. return value == null ? null : fromJson(value, typeReference);
  58. }
  59. public <T> T get(String key, Class<T> clazz) {
  60. return get(key, clazz, NOT_EXPIRE);
  61. }
  62. public <T> T get(String key, TypeReference<T> typeReference) {
  63. return get(key, typeReference, NOT_EXPIRE);
  64. }
  65. public String get(String key, long expire) {
  66. String value = valueOperations.get(key);
  67. if (expire != NOT_EXPIRE) {
  68. redisTemplate.expire(key, expire, TimeUnit.SECONDS);
  69. }
  70. return value;
  71. }
  72. public String get(String key) {
  73. return get(key, NOT_EXPIRE);
  74. }
  75. public Boolean containsKey(String key) {
  76. return redisTemplate.hasKey(key);
  77. }
  78. public Long getExpire(String key) {
  79. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  80. }
  81. public Boolean isExpired(String key) {
  82. return redisTemplate.getExpire(key) > 0;
  83. }
  84. public Long incr(String key, long delta) {
  85. if (delta < 0) {
  86. throw new MyException("递增因子必须大于0");
  87. }
  88. return valueOperations.increment(key, delta);
  89. }
  90. public Long decr(String key, long delta) {
  91. if (delta < 0) {
  92. throw new MyException("递减因子必须大于0");
  93. }
  94. return valueOperations.increment(key, -delta);
  95. }
  96. public void delete(String key) {
  97. redisTemplate.delete(key);
  98. }
  99. /**
  100. * 批量删除
  101. *
  102. * @param key
  103. */
  104. public void deleteBatch(String key) {
  105. if (key != null) {
  106. Set<String> keys = redisTemplate.keys(Pattern.matches("\\*$", key) ? key : key + "*");
  107. if (keys != null && keys.size() > 0) {
  108. redisTemplate.delete(keys);
  109. }
  110. }
  111. }
  112. /**
  113. * Object转成JSON数据
  114. */
  115. private String toJson(Object object) {
  116. if (object instanceof Integer || object instanceof Long || object instanceof Float ||
  117. object instanceof Double || object instanceof Boolean || object instanceof String) {
  118. return String.valueOf(object);
  119. }
  120. return GSON.toJson(object);
  121. // return JSONUtil.toJsonStr(object);
  122. }
  123. /**
  124. * JSON数据,转成Object
  125. */
  126. private <T> T fromJson(String json, Class<T> clazz) {
  127. return JSONUtil.toBean(json, clazz, true);
  128. }
  129. /**
  130. * JSON数据,转成Object
  131. */
  132. private <T> T fromJson(String json, TypeReference<T> typeReference) {
  133. return JSONUtil.toBean(json, typeReference, true);
  134. }
  135. }