RedisUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. package com.genersoft.iot.vmp.utils.redis;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.concurrent.TimeUnit;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.util.CollectionUtils;
  11. /**
  12. * @Description:Redis工具类
  13. * @author: songww
  14. * @date: 2020年5月6日 下午8:27:29
  15. */
  16. @Component
  17. public class RedisUtil {
  18. @Autowired
  19. private RedisTemplate redisTemplate;
  20. /**
  21. * 指定缓存失效时间
  22. * @param key 键
  23. * @param time 时间(秒)
  24. * @return true / false
  25. */
  26. public boolean expire(String key, long time) {
  27. try {
  28. if (time > 0) {
  29. redisTemplate.expire(key, time, TimeUnit.SECONDS);
  30. }
  31. return true;
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. return false;
  35. }
  36. }
  37. /**
  38. * 根据 key 获取过期时间
  39. * @param key 键
  40. * @return
  41. */
  42. public long getExpire(String key) {
  43. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  44. }
  45. /**
  46. * 判断 key 是否存在
  47. * @param key 键
  48. * @return true / false
  49. */
  50. public boolean hasKey(String key) {
  51. try {
  52. return redisTemplate.hasKey(key);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. return false;
  56. }
  57. }
  58. /**
  59. * 删除缓存
  60. * @SuppressWarnings("unchecked") 忽略类型转换警告
  61. * @param key 键(一个或者多个)
  62. */
  63. public boolean del(String... key) {
  64. try {
  65. if (key != null && key.length > 0) {
  66. if (key.length == 1) {
  67. redisTemplate.delete(key[0]);
  68. } else {
  69. // 传入一个 Collection<String> 集合
  70. redisTemplate.delete(CollectionUtils.arrayToList(key));
  71. }
  72. }
  73. return true;
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. return false;
  77. }
  78. }
  79. // ============================== String ==============================
  80. /**
  81. * 普通缓存获取
  82. * @param key 键
  83. * @return 值
  84. */
  85. public Object get(String key) {
  86. return key == null ? null : redisTemplate.opsForValue().get(key);
  87. }
  88. /**
  89. * 普通缓存放入
  90. * @param key 键
  91. * @param value 值
  92. * @return true / false
  93. */
  94. public boolean set(String key, Object value) {
  95. try {
  96. redisTemplate.opsForValue().set(key, value);
  97. return true;
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. return false;
  101. }
  102. }
  103. /**
  104. * 普通缓存放入并设置时间
  105. * @param key 键
  106. * @param value 值
  107. * @param time 时间(秒),如果 time < 0 则设置无限时间
  108. * @return true / false
  109. */
  110. public boolean set(String key, Object value, long time) {
  111. try {
  112. if (time > 0) {
  113. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  114. } else {
  115. set(key, value);
  116. }
  117. return true;
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return false;
  121. }
  122. }
  123. /**
  124. * 递增
  125. * @param key 键
  126. * @param delta 递增大小
  127. * @return
  128. */
  129. public long incr(String key, long delta) {
  130. if (delta < 0) {
  131. throw new RuntimeException("递增因子必须大于 0");
  132. }
  133. return redisTemplate.opsForValue().increment(key, delta);
  134. }
  135. /**
  136. * 递减
  137. * @param key 键
  138. * @param delta 递减大小
  139. * @return
  140. */
  141. public long decr(String key, long delta) {
  142. if (delta < 0) {
  143. throw new RuntimeException("递减因子必须大于 0");
  144. }
  145. return redisTemplate.opsForValue().increment(key, delta);
  146. }
  147. // ============================== Map ==============================
  148. /**
  149. * HashGet
  150. * @param key 键(no null)
  151. * @param item 项(no null)
  152. * @return 值
  153. */
  154. public Object hget(String key, String item) {
  155. return redisTemplate.opsForHash().get(key, item);
  156. }
  157. /**
  158. * 获取 key 对应的 map
  159. * @param key 键(no null)
  160. * @return 对应的多个键值
  161. */
  162. public Map<Object, Object> hmget(String key) {
  163. return redisTemplate.opsForHash().entries(key);
  164. }
  165. /**
  166. * HashSet
  167. * @param key 键
  168. * @param map 值
  169. * @return true / false
  170. */
  171. public boolean hmset(String key, Map<Object, Object> map) {
  172. try {
  173. redisTemplate.opsForHash().putAll(key, map);
  174. return true;
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. return false;
  178. }
  179. }
  180. /**
  181. * HashSet 并设置时间
  182. * @param key 键
  183. * @param map 值
  184. * @param time 时间
  185. * @return true / false
  186. */
  187. public boolean hmset(String key, Map<Object, Object> map, long time) {
  188. try {
  189. redisTemplate.opsForHash().putAll(key, map);
  190. if (time > 0) {
  191. expire(key, time);
  192. }
  193. return true;
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. return false;
  197. }
  198. }
  199. /**
  200. * 向一张 Hash表 中放入数据,如不存在则创建
  201. * @param key 键
  202. * @param item 项
  203. * @param value 值
  204. * @return true / false
  205. */
  206. public boolean hset(String key, String item, Object value) {
  207. try {
  208. redisTemplate.opsForHash().put(key, item, value);
  209. return true;
  210. } catch (Exception e) {
  211. e.printStackTrace();
  212. return false;
  213. }
  214. }
  215. /**
  216. * 向一张 Hash表 中放入数据,并设置时间,如不存在则创建
  217. * @param key 键
  218. * @param item 项
  219. * @param value 值
  220. * @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖)
  221. * @return true / false
  222. */
  223. public boolean hset(String key, String item, Object value, long time) {
  224. try {
  225. redisTemplate.opsForHash().put(key, item, value);
  226. if (time > 0) {
  227. expire(key, time);
  228. }
  229. return true;
  230. } catch (Exception e) {
  231. e.printStackTrace();
  232. return false;
  233. }
  234. }
  235. /**
  236. * 删除 Hash表 中的值
  237. * @param key 键
  238. * @param item 项(可以多个,no null)
  239. */
  240. public void hdel(String key, Object... item) {
  241. redisTemplate.opsForHash().delete(key, item);
  242. }
  243. /**
  244. * 判断 Hash表 中是否有该键的值
  245. * @param key 键(no null)
  246. * @param item 值(no null)
  247. * @return true / false
  248. */
  249. public boolean hHasKey(String key, String item) {
  250. return redisTemplate.opsForHash().hasKey(key, item);
  251. }
  252. /**
  253. * Hash递增,如果不存在则创建一个,并把新增的值返回
  254. * @param key 键
  255. * @param item 项
  256. * @param by 递增大小 > 0
  257. * @return
  258. */
  259. public Double hincr(String key, String item, Double by) {
  260. return redisTemplate.opsForHash().increment(key, item, by);
  261. }
  262. /**
  263. * Hash递减
  264. * @param key 键
  265. * @param item 项
  266. * @param by 递减大小
  267. * @return
  268. */
  269. public Double hdecr(String key, String item, Double by) {
  270. return redisTemplate.opsForHash().increment(key, item, -by);
  271. }
  272. // ============================== Set ==============================
  273. /**
  274. * 根据 key 获取 set 中的所有值
  275. * @param key 键
  276. * @return 值
  277. */
  278. public Set<Object> sGet(String key) {
  279. try {
  280. return redisTemplate.opsForSet().members(key);
  281. } catch (Exception e) {
  282. e.printStackTrace();
  283. return null;
  284. }
  285. }
  286. /**
  287. * 从键为 key 的 set 中,根据 value 查询是否存在
  288. * @param key 键
  289. * @param value 值
  290. * @return true / false
  291. */
  292. public boolean sHasKey(String key, Object value) {
  293. try {
  294. return redisTemplate.opsForSet().isMember(key, value);
  295. } catch (Exception e) {
  296. e.printStackTrace();
  297. return false;
  298. }
  299. }
  300. /**
  301. * 将数据放入 set缓存
  302. * @param key 键值
  303. * @param values 值(可以多个)
  304. * @return 成功个数
  305. */
  306. public long sSet(String key, Object... values) {
  307. try {
  308. return redisTemplate.opsForSet().add(key, values);
  309. } catch (Exception e) {
  310. e.printStackTrace();
  311. return 0;
  312. }
  313. }
  314. /**
  315. * 将数据放入 set缓存,并设置时间
  316. * @param key 键
  317. * @param time 时间
  318. * @param values 值(可以多个)
  319. * @return 成功放入个数
  320. */
  321. public long sSet(String key, long time, Object... values) {
  322. try {
  323. long count = redisTemplate.opsForSet().add(key, values);
  324. if (time > 0) {
  325. expire(key, time);
  326. }
  327. return count;
  328. } catch (Exception e) {
  329. e.printStackTrace();
  330. return 0;
  331. }
  332. }
  333. /**
  334. * 获取 set缓存的长度
  335. * @param key 键
  336. * @return 长度
  337. */
  338. public long sGetSetSize(String key) {
  339. try {
  340. return redisTemplate.opsForSet().size(key);
  341. } catch (Exception e) {
  342. e.printStackTrace();
  343. return 0;
  344. }
  345. }
  346. /**
  347. * 移除 set缓存中,值为 value 的
  348. * @param key 键
  349. * @param values 值
  350. * @return 成功移除个数
  351. */
  352. public long setRemove(String key, Object... values) {
  353. try {
  354. return redisTemplate.opsForSet().remove(key, values);
  355. } catch (Exception e) {
  356. e.printStackTrace();
  357. return 0;
  358. }
  359. }
  360. // ============================== List ==============================
  361. /**
  362. * 获取 list缓存的内容
  363. * @param key 键
  364. * @param start 开始
  365. * @param end 结束(0 到 -1 代表所有值)
  366. * @return
  367. */
  368. public List<Object> lGet(String key, long start, long end) {
  369. try {
  370. return redisTemplate.opsForList().range(key, start, end);
  371. } catch (Exception e) {
  372. e.printStackTrace();
  373. return null;
  374. }
  375. }
  376. /**
  377. * 获取 list缓存的长度
  378. * @param key 键
  379. * @return 长度
  380. */
  381. public long lGetListSize(String key) {
  382. try {
  383. return redisTemplate.opsForList().size(key);
  384. } catch (Exception e) {
  385. e.printStackTrace();
  386. return 0;
  387. }
  388. }
  389. /**
  390. * 根据索引 index 获取键为 key 的 list 中的元素
  391. * @param key 键
  392. * @param index 索引
  393. * 当 index >= 0 时 {0:表头, 1:第二个元素}
  394. * 当 index < 0 时 {-1:表尾, -2:倒数第二个元素}
  395. * @return 值
  396. */
  397. public Object lGetIndex(String key, long index) {
  398. try {
  399. return redisTemplate.opsForList().index(key, index);
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. return null;
  403. }
  404. }
  405. /**
  406. * 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list
  407. * @param key 键
  408. * @param value 值
  409. * @return true / false
  410. */
  411. public boolean lSet(String key, Object value) {
  412. try {
  413. redisTemplate.opsForList().rightPush(key, value);
  414. return true;
  415. } catch (Exception e) {
  416. e.printStackTrace();
  417. return false;
  418. }
  419. }
  420. /**
  421. * 将值 value 插入键为 key 的 list 中,并设置时间
  422. * @param key 键
  423. * @param value 值
  424. * @param time 时间
  425. * @return true / false
  426. */
  427. public boolean lSet(String key, Object value, long time) {
  428. try {
  429. redisTemplate.opsForList().rightPush(key, value);
  430. if (time > 0) {
  431. expire(key, time);
  432. }
  433. return true;
  434. } catch (Exception e) {
  435. e.printStackTrace();
  436. return false;
  437. }
  438. }
  439. /**
  440. * 将 values 插入键为 key 的 list 中
  441. * @param key 键
  442. * @param values 值
  443. * @return true / false
  444. */
  445. public boolean lSetList(String key, List<Object> values) {
  446. try {
  447. redisTemplate.opsForList().rightPushAll(key, values);
  448. return true;
  449. } catch (Exception e) {
  450. e.printStackTrace();
  451. return false;
  452. }
  453. }
  454. /**
  455. * 将 values 插入键为 key 的 list 中,并设置时间
  456. * @param key 键
  457. * @param values 值
  458. * @param time 时间
  459. * @return true / false
  460. */
  461. public boolean lSetList(String key, List<Object> values, long time) {
  462. try {
  463. redisTemplate.opsForList().rightPushAll(key, values);
  464. if (time > 0) {
  465. expire(key, time);
  466. }
  467. return true;
  468. } catch (Exception e) {
  469. e.printStackTrace();
  470. return false;
  471. }
  472. }
  473. /**
  474. * 根据索引 index 修改键为 key 的值
  475. * @param key 键
  476. * @param index 索引
  477. * @param value 值
  478. * @return true / false
  479. */
  480. public boolean lUpdateIndex(String key, long index, Object value) {
  481. try {
  482. redisTemplate.opsForList().set(key, index, value);
  483. return true;
  484. } catch (Exception e) {
  485. e.printStackTrace();
  486. return false;
  487. }
  488. }
  489. /**
  490. * 在键为 key 的 list 中删除值为 value 的元素
  491. * @param key 键
  492. * @param count 如果 count == 0 则删除 list 中所有值为 value 的元素
  493. * 如果 count > 0 则删除 list 中最左边那个值为 value 的元素
  494. * 如果 count < 0 则删除 list 中最右边那个值为 value 的元素
  495. * @param value
  496. * @return
  497. */
  498. public long lRemove(String key, long count, Object value) {
  499. try {
  500. return redisTemplate.opsForList().remove(key, count, value);
  501. } catch (Exception e) {
  502. e.printStackTrace();
  503. return 0;
  504. }
  505. }
  506. /**
  507. * 模糊查询
  508. * @param key 键
  509. * @return true / false
  510. */
  511. public List<Object> keys(String key) {
  512. try {
  513. Set<String> set = redisTemplate.keys(key);
  514. return new ArrayList<>(set);
  515. } catch (Exception e) {
  516. e.printStackTrace();
  517. return null;
  518. }
  519. }
  520. }