RedisUtil.java 12 KB

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