RedisCache.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.ruoyi.framework.redis;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.concurrent.TimeUnit;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.redis.core.BoundSetOperations;
  12. import org.springframework.data.redis.core.HashOperations;
  13. import org.springframework.data.redis.core.ListOperations;
  14. import org.springframework.data.redis.core.RedisTemplate;
  15. import org.springframework.data.redis.core.ValueOperations;
  16. import org.springframework.stereotype.Component;
  17. /**
  18. * spring redis 工具类
  19. *
  20. * @author ruoyi
  21. **/
  22. @SuppressWarnings(value = { "unchecked", "rawtypes" })
  23. @Component
  24. public class RedisCache
  25. {
  26. @Autowired
  27. public RedisTemplate redisTemplate;
  28. /**
  29. * 缓存基本的对象,Integer、String、实体类等
  30. *
  31. * @param key 缓存的键值
  32. * @param value 缓存的值
  33. * @return 缓存的对象
  34. */
  35. public <T> ValueOperations<String, T> setCacheObject(String key, T value)
  36. {
  37. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  38. operation.set(key, value);
  39. return operation;
  40. }
  41. /**
  42. * 缓存基本的对象,Integer、String、实体类等
  43. *
  44. * @param key 缓存的键值
  45. * @param value 缓存的值
  46. * @param timeout 时间
  47. * @param timeUnit 时间颗粒度
  48. * @return 缓存的对象
  49. */
  50. public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit)
  51. {
  52. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  53. operation.set(key, value, timeout, timeUnit);
  54. return operation;
  55. }
  56. /**
  57. * 获得缓存的基本对象。
  58. *
  59. * @param key 缓存键值
  60. * @return 缓存键值对应的数据
  61. */
  62. public <T> T getCacheObject(String key)
  63. {
  64. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  65. return operation.get(key);
  66. }
  67. /**
  68. * 删除单个对象
  69. *
  70. * @param key
  71. */
  72. public void deleteObject(String key)
  73. {
  74. redisTemplate.delete(key);
  75. }
  76. /**
  77. * 删除集合对象
  78. *
  79. * @param collection
  80. */
  81. public void deleteObject(Collection collection)
  82. {
  83. redisTemplate.delete(collection);
  84. }
  85. /**
  86. * 缓存List数据
  87. *
  88. * @param key 缓存的键值
  89. * @param dataList 待缓存的List数据
  90. * @return 缓存的对象
  91. */
  92. public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList)
  93. {
  94. ListOperations listOperation = redisTemplate.opsForList();
  95. if (null != dataList)
  96. {
  97. int size = dataList.size();
  98. for (int i = 0; i < size; i++)
  99. {
  100. listOperation.leftPush(key, dataList.get(i));
  101. }
  102. }
  103. return listOperation;
  104. }
  105. /**
  106. * 获得缓存的list对象
  107. *
  108. * @param key 缓存的键值
  109. * @return 缓存键值对应的数据
  110. */
  111. public <T> List<T> getCacheList(String key)
  112. {
  113. List<T> dataList = new ArrayList<T>();
  114. ListOperations<String, T> listOperation = redisTemplate.opsForList();
  115. Long size = listOperation.size(key);
  116. for (int i = 0; i < size; i++)
  117. {
  118. dataList.add(listOperation.index(key, i));
  119. }
  120. return dataList;
  121. }
  122. /**
  123. * 缓存Set
  124. *
  125. * @param key 缓存键值
  126. * @param dataSet 缓存的数据
  127. * @return 缓存数据的对象
  128. */
  129. public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet)
  130. {
  131. BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
  132. Iterator<T> it = dataSet.iterator();
  133. while (it.hasNext())
  134. {
  135. setOperation.add(it.next());
  136. }
  137. return setOperation;
  138. }
  139. /**
  140. * 获得缓存的set
  141. *
  142. * @param key
  143. * @return
  144. */
  145. public <T> Set<T> getCacheSet(String key)
  146. {
  147. Set<T> dataSet = new HashSet<T>();
  148. BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
  149. Long size = operation.size();
  150. for (int i = 0; i < size; i++)
  151. {
  152. dataSet.add(operation.pop());
  153. }
  154. return dataSet;
  155. }
  156. /**
  157. * 缓存Map
  158. *
  159. * @param key
  160. * @param dataMap
  161. * @return
  162. */
  163. public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap)
  164. {
  165. HashOperations hashOperations = redisTemplate.opsForHash();
  166. if (null != dataMap)
  167. {
  168. for (Map.Entry<String, T> entry : dataMap.entrySet())
  169. {
  170. hashOperations.put(key, entry.getKey(), entry.getValue());
  171. }
  172. }
  173. return hashOperations;
  174. }
  175. /**
  176. * 获得缓存的Map
  177. *
  178. * @param key
  179. * @return
  180. */
  181. public <T> Map<String, T> getCacheMap(String key)
  182. {
  183. Map<String, T> map = redisTemplate.opsForHash().entries(key);
  184. return map;
  185. }
  186. /**
  187. * 获得缓存的基本对象列表
  188. *
  189. * @param pattern 字符串前缀
  190. * @return 对象列表
  191. */
  192. public Collection<String> keys(String pattern)
  193. {
  194. return redisTemplate.keys(pattern);
  195. }
  196. }