导读


由于项目需要,整合下Redis模板工具类,用来处理缓存相关的操作。

使用


依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

Redis缓存

Service

  1. import java.util.List;
  2. import java.util.Map;
  3. import java.util.Set;
  4. /**
  5. * redis操作Service接口
  6. */
  7. public interface RedisService {
  8. /**
  9. * 保存属性
  10. */
  11. void set(String key, Object value, long time);
  12. /**
  13. * 保存属性
  14. */
  15. void set(String key, Object value);
  16. /**
  17. * 获取属性
  18. */
  19. Object get(String key);
  20. /**
  21. * 删除属性
  22. */
  23. Boolean del(String key);
  24. /**
  25. * 批量删除属性
  26. */
  27. Long del(List<String> keys);
  28. /**
  29. * 设置过期时间
  30. */
  31. Boolean expire(String key, long time);
  32. /**
  33. * 获取过期时间
  34. */
  35. Long getExpire(String key);
  36. /**
  37. * 判断是否有该属性
  38. */
  39. Boolean hasKey(String key);
  40. /**
  41. * 按delta递增
  42. */
  43. Long incr(String key, long delta);
  44. /**
  45. * 按delta递减
  46. */
  47. Long decr(String key, long delta);
  48. /**
  49. * 获取Hash结构中的属性
  50. */
  51. Object hGet(String key, String hashKey);
  52. /**
  53. * 向Hash结构中放入一个属性
  54. */
  55. Boolean hSet(String key, String hashKey, Object value, long time);
  56. /**
  57. * 向Hash结构中放入一个属性
  58. */
  59. void hSet(String key, String hashKey, Object value);
  60. /**
  61. * 直接获取整个Hash结构
  62. */
  63. Map<Object, Object> hGetAll(String key);
  64. /**
  65. * 直接设置整个Hash结构
  66. */
  67. Boolean hSetAll(String key, Map<String, Object> map, long time);
  68. /**
  69. * 直接设置整个Hash结构
  70. */
  71. void hSetAll(String key, Map<String, ?> map);
  72. /**
  73. * 删除Hash结构中的属性
  74. */
  75. void hDel(String key, Object... hashKey);
  76. /**
  77. * 判断Hash结构中是否有该属性
  78. */
  79. Boolean hHasKey(String key, String hashKey);
  80. /**
  81. * Hash结构中属性递增
  82. */
  83. Long hIncr(String key, String hashKey, Long delta);
  84. /**
  85. * Hash结构中属性递减
  86. */
  87. Long hDecr(String key, String hashKey, Long delta);
  88. /**
  89. * 获取Set结构
  90. */
  91. Set<Object> sMembers(String key);
  92. /**
  93. * 向Set结构中添加属性
  94. */
  95. Long sAdd(String key, Object... values);
  96. /**
  97. * 向Set结构中添加属性
  98. */
  99. Long sAdd(String key, long time, Object... values);
  100. /**
  101. * 是否为Set中的属性
  102. */
  103. Boolean sIsMember(String key, Object value);
  104. /**
  105. * 获取Set结构的长度
  106. */
  107. Long sSize(String key);
  108. /**
  109. * 删除Set结构中的属性
  110. */
  111. Long sRemove(String key, Object... values);
  112. /**
  113. * 获取List结构中的属性
  114. */
  115. List<Object> lRange(String key, long start, long end);
  116. /**
  117. * 获取List结构的长度
  118. */
  119. Long lSize(String key);
  120. /**
  121. * 根据索引获取List中的属性
  122. */
  123. Object lIndex(String key, long index);
  124. /**
  125. * 向List结构中添加属性
  126. */
  127. Long lPush(String key, Object value);
  128. /**
  129. * 向List结构中添加属性
  130. */
  131. Long lPush(String key, Object value, long time);
  132. /**
  133. * 向List结构中批量添加属性
  134. */
  135. Long lPushAll(String key, Object... values);
  136. /**
  137. * 向List结构中批量添加属性
  138. */
  139. Long lPushAll(String key, Long time, Object... values);
  140. /**
  141. * 从List结构中移除属性
  142. */
  143. Long lRemove(String key, long count, Object value);
  144. }

ServiceImpl

  1. import com.macro.mall.common.service.RedisService;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.stereotype.Service;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.concurrent.TimeUnit;
  9. /**
  10. * redis操作实现类处理相关缓存操作
  11. */
  12. public class RedisServiceImpl implements RedisService {
  13. @Autowired
  14. private RedisTemplate<String, Object> redisTemplate;
  15. @Override
  16. public void set(String key, Object value, long time) {
  17. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  18. }
  19. @Override
  20. public void set(String key, Object value) {
  21. redisTemplate.opsForValue().set(key, value);
  22. }
  23. @Override
  24. public Object get(String key) {
  25. return redisTemplate.opsForValue().get(key);
  26. }
  27. @Override
  28. public Boolean del(String key) {
  29. return redisTemplate.delete(key);
  30. }
  31. @Override
  32. public Long del(List<String> keys) {
  33. return redisTemplate.delete(keys);
  34. }
  35. @Override
  36. public Boolean expire(String key, long time) {
  37. return redisTemplate.expire(key, time, TimeUnit.SECONDS);
  38. }
  39. @Override
  40. public Long getExpire(String key) {
  41. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  42. }
  43. @Override
  44. public Boolean hasKey(String key) {
  45. return redisTemplate.hasKey(key);
  46. }
  47. @Override
  48. public Long incr(String key, long delta) {
  49. return redisTemplate.opsForValue().increment(key, delta);
  50. }
  51. @Override
  52. public Long decr(String key, long delta) {
  53. return redisTemplate.opsForValue().increment(key, -delta);
  54. }
  55. @Override
  56. public Object hGet(String key, String hashKey) {
  57. return redisTemplate.opsForHash().get(key, hashKey);
  58. }
  59. @Override
  60. public Boolean hSet(String key, String hashKey, Object value, long time) {
  61. redisTemplate.opsForHash().put(key, hashKey, value);
  62. return expire(key, time);
  63. }
  64. @Override
  65. public void hSet(String key, String hashKey, Object value) {
  66. redisTemplate.opsForHash().put(key, hashKey, value);
  67. }
  68. @Override
  69. public Map<Object, Object> hGetAll(String key) {
  70. return redisTemplate.opsForHash().entries(key);
  71. }
  72. @Override
  73. public Boolean hSetAll(String key, Map<String, Object> map, long time) {
  74. redisTemplate.opsForHash().putAll(key, map);
  75. return expire(key, time);
  76. }
  77. @Override
  78. public void hSetAll(String key, Map<String, ?> map) {
  79. redisTemplate.opsForHash().putAll(key, map);
  80. }
  81. @Override
  82. public void hDel(String key, Object... hashKey) {
  83. redisTemplate.opsForHash().delete(key, hashKey);
  84. }
  85. @Override
  86. public Boolean hHasKey(String key, String hashKey) {
  87. return redisTemplate.opsForHash().hasKey(key, hashKey);
  88. }
  89. @Override
  90. public Long hIncr(String key, String hashKey, Long delta) {
  91. return redisTemplate.opsForHash().increment(key, hashKey, delta);
  92. }
  93. @Override
  94. public Long hDecr(String key, String hashKey, Long delta) {
  95. return redisTemplate.opsForHash().increment(key, hashKey, -delta);
  96. }
  97. @Override
  98. public Set<Object> sMembers(String key) {
  99. return redisTemplate.opsForSet().members(key);
  100. }
  101. @Override
  102. public Long sAdd(String key, Object... values) {
  103. return redisTemplate.opsForSet().add(key, values);
  104. }
  105. @Override
  106. public Long sAdd(String key, long time, Object... values) {
  107. Long count = redisTemplate.opsForSet().add(key, values);
  108. expire(key, time);
  109. return count;
  110. }
  111. @Override
  112. public Boolean sIsMember(String key, Object value) {
  113. return redisTemplate.opsForSet().isMember(key, value);
  114. }
  115. @Override
  116. public Long sSize(String key) {
  117. return redisTemplate.opsForSet().size(key);
  118. }
  119. @Override
  120. public Long sRemove(String key, Object... values) {
  121. return redisTemplate.opsForSet().remove(key, values);
  122. }
  123. @Override
  124. public List<Object> lRange(String key, long start, long end) {
  125. return redisTemplate.opsForList().range(key, start, end);
  126. }
  127. @Override
  128. public Long lSize(String key) {
  129. return redisTemplate.opsForList().size(key);
  130. }
  131. @Override
  132. public Object lIndex(String key, long index) {
  133. return redisTemplate.opsForList().index(key, index);
  134. }
  135. @Override
  136. public Long lPush(String key, Object value) {
  137. return redisTemplate.opsForList().rightPush(key, value);
  138. }
  139. @Override
  140. public Long lPush(String key, Object value, long time) {
  141. Long index = redisTemplate.opsForList().rightPush(key, value);
  142. expire(key, time);
  143. return index;
  144. }
  145. @Override
  146. public Long lPushAll(String key, Object... values) {
  147. return redisTemplate.opsForList().rightPushAll(key, values);
  148. }
  149. @Override
  150. public Long lPushAll(String key, Long time, Object... values) {
  151. Long count = redisTemplate.opsForList().rightPushAll(key, values);
  152. expire(key, time);
  153. return count;
  154. }
  155. @Override
  156. public Long lRemove(String key, long count, Object value) {
  157. return redisTemplate.opsForList().remove(key, count, value);
  158. }
  159. }

END


搞定~