50ad7d7fd72ad7293a17359bf8a281bbc5eb9797.svn-base 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package cn.com.goldenwater.dcproj.utils;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.BoundSetOperations;
  4. import org.springframework.data.redis.core.HashOperations;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.core.ValueOperations;
  7. import org.springframework.stereotype.Component;
  8. import java.util.*;
  9. import java.util.concurrent.TimeUnit;
  10. /**
  11. * spring redis 工具类
  12. *
  13. * @author ruoyi
  14. **/
  15. @SuppressWarnings(value = { "unchecked", "rawtypes" })
  16. @Component
  17. public class RedisCache
  18. {
  19. @Autowired
  20. public RedisTemplate redisTemplate;
  21. /**
  22. * 缓存基本的对象,Integer、String、实体类等
  23. *
  24. * @param key 缓存的键值
  25. * @param value 缓存的值
  26. */
  27. public <T> void setCacheObject(final String key, final T value)
  28. {
  29. redisTemplate.opsForValue().set(key, value);
  30. }
  31. /**
  32. * 缓存基本的对象,Integer、String、实体类等
  33. *
  34. * @param key 缓存的键值
  35. * @param value 缓存的值
  36. * @param timeout 时间
  37. * @param timeUnit 时间颗粒度
  38. */
  39. public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
  40. {
  41. redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
  42. }
  43. /**
  44. * 设置有效时间
  45. *
  46. * @param key Redis键
  47. * @param timeout 超时时间
  48. * @return true=设置成功;false=设置失败
  49. */
  50. public boolean expire(final String key, final long timeout)
  51. {
  52. return expire(key, timeout, TimeUnit.SECONDS);
  53. }
  54. /**
  55. * 设置有效时间
  56. *
  57. * @param key Redis键
  58. * @param timeout 超时时间
  59. * @param unit 时间单位
  60. * @return true=设置成功;false=设置失败
  61. */
  62. public boolean expire(final String key, final long timeout, final TimeUnit unit)
  63. {
  64. return redisTemplate.expire(key, timeout, unit);
  65. }
  66. /**
  67. * 获得缓存的基本对象。
  68. *
  69. * @param key 缓存键值
  70. * @return 缓存键值对应的数据
  71. */
  72. public <T> T getCacheObject(final String key)
  73. {
  74. ValueOperations<String, T> operation = redisTemplate.opsForValue();
  75. return operation.get(key);
  76. }
  77. /**
  78. * 删除单个对象
  79. *
  80. * @param key
  81. */
  82. public boolean deleteObject(final String key)
  83. {
  84. return redisTemplate.delete(key);
  85. }
  86. /**
  87. * 删除集合对象
  88. *
  89. * @param collection 多个对象
  90. * @return
  91. */
  92. public long deleteObject(final Collection collection)
  93. {
  94. return redisTemplate.delete(collection);
  95. }
  96. /**
  97. * 缓存List数据
  98. *
  99. * @param key 缓存的键值
  100. * @param dataList 待缓存的List数据
  101. * @return 缓存的对象
  102. */
  103. public <T> long setCacheList(final String key, final List<T> dataList)
  104. {
  105. Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
  106. return count == null ? 0 : count;
  107. }
  108. /**
  109. * 获得缓存的list对象
  110. *
  111. * @param key 缓存的键值
  112. * @return 缓存键值对应的数据
  113. */
  114. public <T> List<T> getCacheList(final String key)
  115. {
  116. return redisTemplate.opsForList().range(key, 0, -1);
  117. }
  118. /**
  119. * 缓存Set
  120. *
  121. * @param key 缓存键值
  122. * @param dataSet 缓存的数据
  123. * @return 缓存数据的对象
  124. */
  125. public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
  126. {
  127. BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
  128. Iterator<T> it = dataSet.iterator();
  129. while (it.hasNext())
  130. {
  131. setOperation.add(it.next());
  132. }
  133. return setOperation;
  134. }
  135. /**
  136. * 获得缓存的set
  137. *
  138. * @param key
  139. * @return
  140. */
  141. public <T> Set<T> getCacheSet(final String key)
  142. {
  143. return redisTemplate.opsForSet().members(key);
  144. }
  145. /**
  146. * 缓存Map
  147. *
  148. * @param key
  149. * @param dataMap
  150. */
  151. public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
  152. {
  153. if (dataMap != null) {
  154. redisTemplate.opsForHash().putAll(key, dataMap);
  155. }
  156. }
  157. /**
  158. * 获得缓存的Map
  159. *
  160. * @param key
  161. * @return
  162. */
  163. public <T> Map<String, T> getCacheMap(final String key)
  164. {
  165. return redisTemplate.opsForHash().entries(key);
  166. }
  167. /**
  168. * 往Hash中存入数据
  169. *
  170. * @param key Redis键
  171. * @param hKey Hash键
  172. * @param value 值
  173. */
  174. public <T> void setCacheMapValue(final String key, final String hKey, final T value)
  175. {
  176. redisTemplate.opsForHash().put(key, hKey, value);
  177. }
  178. /**
  179. * 获取Hash中的数据
  180. *
  181. * @param key Redis键
  182. * @param hKey Hash键
  183. * @return Hash中的对象
  184. */
  185. public <T> T getCacheMapValue(final String key, final String hKey)
  186. {
  187. HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
  188. return opsForHash.get(key, hKey);
  189. }
  190. /**
  191. * 删除Hash中的数据
  192. *
  193. * @param key
  194. * @param mapkey
  195. */
  196. public void delCacheMapValue(final String key, final String hkey)
  197. {
  198. HashOperations hashOperations = redisTemplate.opsForHash();
  199. hashOperations.delete(key, hkey);
  200. }
  201. /**
  202. * 获取多个Hash中的数据
  203. *
  204. * @param key Redis键
  205. * @param hKeys Hash键集合
  206. * @return Hash对象集合
  207. */
  208. public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
  209. {
  210. return redisTemplate.opsForHash().multiGet(key, hKeys);
  211. }
  212. /**
  213. * 获得缓存的基本对象列表
  214. *
  215. * @param pattern 字符串前缀
  216. * @return 对象列表
  217. */
  218. public Collection<String> keys(final String pattern)
  219. {
  220. return redisTemplate.keys(pattern);
  221. }
  222. }