SmartRedisService.java 15 KB

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