1. package com.haier.hsi.workplace.redis;
    2. import com.haier.hsi.framework.commons.util.ListUtil;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.dao.DataAccessException;
    5. import org.springframework.data.redis.core.RedisOperations;
    6. import org.springframework.data.redis.core.RedisTemplate;
    7. import org.springframework.data.redis.core.SessionCallback;
    8. import org.springframework.data.redis.serializer.StringRedisSerializer;
    9. import org.springframework.stereotype.Component;
    10. import javax.annotation.Resource;
    11. import java.util.*;
    12. import java.util.concurrent.TimeUnit;
    13. /**
    14. * redis操作类 所有需要存入redis的对象都必须序列化
    15. *
    16. * @param <K> 目前仅支持String类型
    17. * @author WangHQ
    18. * @date 2017-08-24 13:59
    19. * @param <V>
    20. */
    21. @Component
    22. @SuppressWarnings("unchecked")
    23. public class BaseRedisHelper<K, V> {
    24. @Resource(name = "redisTemplate")
    25. private RedisTemplate<K, V> redisTemplate; // redis服务
    26. private String prefix = "com:haier:hsi:baseData:"; // 前缀
    27. /**
    28. * 构造方法
    29. *
    30. * @author WangHQ
    31. * @date 2017-08-24
    32. */
    33. public BaseRedisHelper() {}
    34. /**
    35. * 设置对象
    36. *
    37. * @param key 键
    38. * @param value 值
    39. * @return 执行结果
    40. * @author WangHQ
    41. * @date 2017-08-24
    42. */
    43. public boolean setObject(K key, V value) {
    44. keySerialize();
    45. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    46. @Override
    47. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    48. redisOperations.multi();
    49. redisOperations.opsForValue().set(prefix + key, value, 60 * 60 * 24, TimeUnit.SECONDS);
    50. return redisOperations.exec();
    51. }
    52. };
    53. redisTemplate.execute(sessionCallback);
    54. return true;
    55. }
    56. /**
    57. * key唯一 如果key存在 就返回false 并且无法插入 lixy add
    58. * */
    59. public boolean setIfAbsent(K key, V value) {
    60. keySerialize();
    61. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    62. @Override
    63. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    64. redisOperations.multi();
    65. Boolean bb = redisOperations.opsForValue().setIfAbsent(prefix + key, value);
    66. List<V> ss = redisOperations.exec();
    67. return ss;
    68. }
    69. };
    70. List<V> sss = (List<V>)redisTemplate.execute(sessionCallback);
    71. V s = sss.get(0);
    72. return (Boolean)s;
    73. }
    74. /**
    75. * key唯一 如果key存在 就返回false 并且无法插入 lixy add
    76. * */
    77. public boolean setIfAbsent(K key, V value, long time) {
    78. keySerialize();
    79. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    80. @Override
    81. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    82. redisOperations.multi();
    83. redisOperations.opsForValue().setIfAbsent(prefix + key, value);
    84. List<V> ss = redisOperations.exec();
    85. System.out.println("ss=" + ss.toString());
    86. V s = ss.get(0);
    87. if((Boolean)s){
    88. redisOperations.multi();
    89. redisOperations.expire(prefix + key, time, TimeUnit.SECONDS);
    90. redisOperations.exec();
    91. System.out.println("time=" + new Date().toString());
    92. }
    93. return ss;
    94. }
    95. };
    96. List<V> sss = (List<V>)redisTemplate.execute(sessionCallback);
    97. V s = sss.get(0);
    98. return (Boolean)s;
    99. }
    100. /**
    101. * 设置对象和生存时效
    102. *
    103. * @param key 键
    104. * @param value 值
    105. * @param time
    106. * @return 执行结果
    107. * @author wayne
    108. * @date 2018-05-11
    109. */
    110. public boolean setObject(K key, V value, long time) {
    111. keySerialize();
    112. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    113. @Override
    114. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    115. redisOperations.multi();
    116. redisOperations.opsForValue().set(prefix + key, value, time, TimeUnit.SECONDS);
    117. return redisOperations.exec();
    118. }
    119. };
    120. redisTemplate.execute(sessionCallback);
    121. return true;
    122. }
    123. /**
    124. * 设置对象和生存时效
    125. *
    126. * @param key 键
    127. * @param value 值
    128. * @param noTimeout
    129. * @return 执行结果
    130. * @author wayne
    131. * @date 2019-05-11
    132. */
    133. public boolean setObject(K key, V value, boolean noTimeout) {
    134. keySerialize();
    135. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    136. @Override
    137. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    138. redisOperations.multi();
    139. redisOperations.opsForValue().set(prefix + key, value);
    140. return redisOperations.exec();
    141. }
    142. };
    143. redisTemplate.execute(sessionCallback);
    144. return true;
    145. }
    146. /**
    147. * 设置对象list
    148. *
    149. * @param map map
    150. * @return 执行结果
    151. * @author WangHQ
    152. * @date 2017-08-24
    153. */
    154. public boolean setObjectList(Map<K, V> map) {
    155. keySerialize();
    156. Map<K, V> setMap = new HashMap<>();
    157. for (Map.Entry<K, V> kvEntry : map.entrySet()) {
    158. setMap.put((K) (prefix + kvEntry.getKey()), kvEntry.getValue());
    159. }
    160. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    161. @Override
    162. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    163. redisOperations.multi();
    164. redisOperations.opsForValue().multiSet(setMap);
    165. return redisOperations.exec();
    166. }
    167. };
    168. redisTemplate.execute(sessionCallback);
    169. return true;
    170. }
    171. /**
    172. * 获取对象
    173. *
    174. * @param key 键
    175. * @return 对象
    176. * @author WangHQ
    177. * @date 2017-08-24
    178. */
    179. public V getObject(K key) {
    180. keySerialize();
    181. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    182. @Override
    183. public V execute(RedisOperations redisOperations) throws DataAccessException {
    184. redisOperations.multi();
    185. redisOperations.opsForValue().get(prefix + key);
    186. List<V> v = redisOperations.exec();
    187. if (ListUtil.isNotEmpty(v)) {
    188. return v.get(0);
    189. } else {
    190. return null;
    191. }
    192. }
    193. };
    194. return redisTemplate.execute(sessionCallback);
    195. }
    196. /**
    197. * 获取对象列表
    198. *
    199. * @param pattern 表达式 keys
    200. * @return 对象
    201. * @author WangHQ
    202. * @date 2017-08-24
    203. */
    204. public List<V> getObjectList(K pattern) {
    205. keySerialize();
    206. Set<K> keys = redisTemplate.keys((K) (prefix + pattern));
    207. // 如果没有相关的key存在,就没有必要进行后续操作了
    208. if (keys.isEmpty()) {
    209. return new ArrayList<>();
    210. }
    211. SessionCallback<List<V>> sessionCallback = new SessionCallback<List<V>>() {
    212. @Override
    213. public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
    214. redisOperations.multi();
    215. redisOperations.opsForValue().multiGet(keys);
    216. List<V> v = redisOperations.exec();
    217. List<V> list = new ArrayList<>();
    218. if (ListUtil.isNotEmpty(v)) {
    219. list.addAll((List<V>) v.get(0));
    220. }
    221. return list;
    222. }
    223. };
    224. return redisTemplate.execute(sessionCallback);
    225. }
    226. /**
    227. * 删除对象
    228. *
    229. * @param key 键
    230. * @return 执行结果
    231. * @author WangHQ
    232. * @date 2017-08-24
    233. */
    234. public boolean deleteObject(K key) {
    235. keySerialize();
    236. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    237. @Override
    238. public List<V> execute(RedisOperations operations) throws DataAccessException {
    239. operations.multi();
    240. operations.delete(prefix + key);
    241. return operations.exec();
    242. }
    243. };
    244. redisTemplate.execute(sessionCallback);
    245. return true;
    246. }
    247. /**
    248. * 根据表达式删除对象
    249. *
    250. * @param pattern 表达式 keys
    251. * @return 执行结果
    252. * @author WangHQ
    253. * @date 2017-08-24
    254. */
    255. public boolean deleteObjectList(K pattern) {
    256. keySerialize();
    257. Set<K> keys = redisTemplate.keys((K) (prefix + pattern));
    258. // 如果没有相关的key存在,就没有必要进行后续操作了
    259. if (keys.isEmpty()) {
    260. return false;
    261. }
    262. SessionCallback<V> sessionCallback = new SessionCallback<V>() {
    263. @Override
    264. public List<V> execute(RedisOperations operations) throws DataAccessException {
    265. operations.multi();
    266. operations.delete(keys);
    267. return operations.exec();
    268. }
    269. };
    270. redisTemplate.execute(sessionCallback);
    271. return true;
    272. }
    273. /**
    274. * 序列化
    275. *
    276. * @author WangHQ
    277. * @date 2017-08-24
    278. */
    279. private void keySerialize() {
    280. redisTemplate.setKeySerializer(new StringRedisSerializer());
    281. }
    282. /**
    283. * 按照表达式查询key列表
    284. * @param pattern
    285. * @return
    286. */
    287. public Set<K> queryKeysByPattern(String pattern) {
    288. return redisTemplate.keys((K) (prefix + pattern + "*"));
    289. }
    290. /**
    291. * 按照表达式模糊查询key列表
    292. * @param patternA,patternB
    293. * @return
    294. */
    295. public Set<K> queryKeysByPattern(String patternA,String patternB) {
    296. return redisTemplate.keys((K) (prefix + patternA + "*"+patternB));
    297. }
    298. }
    299. //~ Formatted by Jindent --- http://www.jindent.com