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