1. package com.fndsoft.bcis.utils;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.*;
  4. import org.springframework.stereotype.Service;
  5. import java.util.*;
  6. /**
  7. * redis缓存帮助类
  8. * Created by DELL on 2016/5/23.
  9. */
  10. @Service
  11. public class RedisCacheUtil<T> {
  12. @Autowired
  13. public RedisTemplate redisTemplate;
  14. /**
  15. * 缓存基本的对象,Integer、String、实体类等
  16. *
  17. * @param key 缓存的键值
  18. * @param value 缓存的值
  19. * @return 缓存的对象
  20. */
  21. public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
  22. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  23. operation.set(key, value);
  24. return operation;
  25. }
  26. /**
  27. * 获得缓存的基本对象。
  28. *
  29. * @param key 缓存键值
  30. * @return 缓存键值对应的数据
  31. */
  32. public <T> T getCacheObject(String key) {
  33. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  34. return operation.get(key);
  35. }
  36. /**
  37. * 缓存List数据
  38. *
  39. * @param key 缓存的键值
  40. * @param dataList 待缓存的List数据
  41. * @return 缓存的对象
  42. */
  43. public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
  44. ListOperations listOperation = redisTemplate.opsForList();
  45. if (null != dataList) {
  46. int size = dataList.size();
  47. for (int i = 0; i < size; i++) {
  48. listOperation.leftPush(key, dataList.get(i));
  49. }
  50. }
  51. return listOperation;
  52. }
  53. /**
  54. * 获得缓存的list对象
  55. *
  56. * @param key 缓存的键值
  57. * @return 缓存键值对应的数据
  58. */
  59. public <T> List<T> getCacheList(String key) {
  60. List<T> dataList = new ArrayList<T>();
  61. ListOperations<String, T> listOperation = redisTemplate.opsForList();
  62. Long size = listOperation.size(key);
  63. for (int i = 0; i < size; i++) {
  64. dataList.add(listOperation.index(key,i));
  65. }
  66. return dataList;
  67. }
  68. /**
  69. * 缓存Set
  70. *
  71. * @param key 缓存键值
  72. * @param dataSet 缓存的数据
  73. * @return 缓存数据的对象
  74. */
  75. public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
  76. BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
  77. Iterator<T> it = dataSet.iterator();
  78. while (it.hasNext()) {
  79. setOperation.add(it.next());
  80. }
  81. return setOperation;
  82. }
  83. /**
  84. * 获得缓存的set
  85. *
  86. * @param key
  87. * @return
  88. */
  89. public Set<T> getCacheSet(String key) {
  90. Set<T> dataSet = new HashSet<T>();
  91. BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
  92. Long size = operation.size();
  93. for (int i = 0; i < size; i++) {
  94. dataSet.add(operation.pop());
  95. }
  96. return dataSet;
  97. }
  98. /**
  99. * 缓存Map
  100. *
  101. * @param key
  102. * @param dataMap
  103. * @return
  104. */
  105. public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) {
  106. HashOperations hashOperations = redisTemplate.opsForHash();
  107. if (null != dataMap) {
  108. for (Map.Entry<String, T> entry : dataMap.entrySet()) {
  109. hashOperations.put(key, entry.getKey(), entry.getValue());
  110. }
  111. }
  112. return hashOperations;
  113. }
  114. /**
  115. * 获得缓存的Map
  116. *
  117. * @param key
  118. * @return
  119. */
  120. public <T> Map<String, T> getCacheMap(String key) {
  121. Map<String, T> map = redisTemplate.opsForHash().entries(key);
  122. return map;
  123. }
  124. /**
  125. * 缓存Map
  126. *
  127. * @param key
  128. * @param dataMap
  129. * @return
  130. */
  131. public <T> HashOperations<String, Integer, T> setCacheIntegerMap(String key, Map<Integer, T> dataMap) {
  132. HashOperations hashOperations = redisTemplate.opsForHash();
  133. if (null != dataMap) {
  134. for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {
  135. hashOperations.put(key, entry.getKey(), entry.getValue());
  136. }
  137. }
  138. return hashOperations;
  139. }
  140. /**
  141. * 获得缓存的Map
  142. *
  143. * @param key
  144. * @return
  145. */
  146. public <T> Map<Integer, T> getCacheIntegerMap(String key) {
  147. Map<Integer, T> map = redisTemplate.opsForHash().entries(key);
  148. return map;
  149. }
  150. }

以下是存储list对象的测试方法:

  1. /**
  2. * redis缓存list对象
  3. */
  4. @Test
  5. public void setCatchValueForList() {
  6. String key = "user_Test7";
  7. List<Code> codeList = new ArrayList<>();
  8. Code code1 = new Code("436436", 533);
  9. Code code2 = new Code("214214", 53453);
  10. codeList.add(code1);
  11. codeList.add(code2);
  12. redisCacheUtil.setCacheList(key, codeList);
  13. System.out.println("保存数据成功!");
  14. }
  15. @Test
  16. public void getValueForList() {
  17. String key = "user_Test7";
  18. List<Code> codeList = redisCacheUtil.getCacheList(key);
  19. for (Code code : codeList) {
  20. System.out.println(code.getName() + " " + code.getAge());
  21. }
  22. }

 切记被缓存的实例化对象必须系列化:

  1. public class Code implements Serializable {}


    另外还可以对将要保存的key设置有效时间,超过此有效时间,该key会自动删除失效:
  1. redisTemplate.expire(key, 1, TimeUnit.HOURS);//此处TimeUnit.HOURS设置有效时长的单位为小时

  

版权声明:本文为baizhanshi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/baizhanshi/p/5527467.html