RedisService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package com.cyksj.redis;
  2. import com.cyksj.common.util.StringUtil;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Getter;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.stereotype.Service;
  8. import javax.annotation.PostConstruct;
  9. import javax.annotation.Resource;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import java.util.concurrent.TimeUnit;
  15. /**
  16. * @description 定义常用的 Redis操作
  17. * @author chan
  18. * @date 2021-03-31 5:34 下午
  19. */
  20. @Service
  21. @SuppressWarnings("all")
  22. public class RedisService {
  23. @Resource
  24. private RedisTemplate<String, Object> redisTemplate;
  25. public static String env = "dev";
  26. @Value("${spring.profiles.active}")
  27. private String active;
  28. @PostConstruct
  29. public void init(){
  30. env = active + ":";
  31. }
  32. /**
  33. * 指定缓存失效时间
  34. *
  35. * @param key 键
  36. * @param time 时间(秒)
  37. * @return Boolean
  38. */
  39. public Boolean expire(String key, Long time) {
  40. try {
  41. if (time > 0) {
  42. redisTemplate.expire(key, time, TimeUnit.SECONDS);
  43. }
  44. return true;
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. return false;
  48. }
  49. }
  50. /**
  51. * 根据key获取过期时间
  52. *
  53. * @param key 键 不能为 null
  54. * @return 时间(秒) 返回 0代表为永久有效
  55. */
  56. public Long getExpire(String key) {
  57. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  58. }
  59. /**
  60. * 判断 key是否存在
  61. *
  62. * @param key 键
  63. * @return true 存在 false不存在
  64. */
  65. public Boolean hasKey(String key) {
  66. try {
  67. return redisTemplate.hasKey(key);
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. return false;
  71. }
  72. }
  73. /**
  74. * 删除缓存
  75. *
  76. * @param key 可以传一个值 或多个
  77. */
  78. public void del(String... key) {
  79. if (key != null && key.length > 0) {
  80. if (key.length == 1) {
  81. redisTemplate.delete(key[0]);
  82. } else {
  83. redisTemplate.delete(Arrays.asList(key));
  84. }
  85. }
  86. }
  87. /**
  88. * 普通缓存获取
  89. *
  90. * @param key 键
  91. * @return 值
  92. */
  93. public Object get(String key) {
  94. return key == null ? null : redisTemplate.opsForValue().get(key);
  95. }
  96. /**
  97. * String缓存获取
  98. * @param key 键
  99. * @return 值
  100. */
  101. public String getStr(String key) {
  102. return key == null ? null : StringUtil.getString(redisTemplate.opsForValue().get(key));
  103. }
  104. /**
  105. * 普通缓存放入
  106. *
  107. * @param key 键
  108. * @param value 值
  109. * @return true成功 false失败
  110. */
  111. public Boolean set(String key, Object value) {
  112. try {
  113. redisTemplate.opsForValue().set(key, value);
  114. return true;
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. return false;
  118. }
  119. }
  120. /**
  121. * 普通缓存放入并设置时间
  122. *
  123. * @param key 键
  124. * @param value 值
  125. * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
  126. * @return true成功 false 失败
  127. */
  128. public Boolean set(String key, Object value, Long time) {
  129. try {
  130. if (time > 0) {
  131. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  132. } else {
  133. set(key, value);
  134. }
  135. return true;
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. return false;
  139. }
  140. }
  141. /**
  142. * 递增
  143. *
  144. * @param key 键
  145. * @param delta 要增加几(大于0)
  146. * @return Long
  147. */
  148. public Long incr(String key, Long delta) {
  149. if (delta < 0) {
  150. throw new RuntimeException("递增因子必须大于0");
  151. }
  152. return redisTemplate.opsForValue().increment(key, delta);
  153. }
  154. /**
  155. * 递减
  156. *
  157. * @param key 键
  158. * @param delta 要减少几
  159. * @return Long
  160. */
  161. public Long decr(String key, Long delta) {
  162. if (delta < 0) {
  163. throw new RuntimeException("递减因子必须大于0");
  164. }
  165. return redisTemplate.opsForValue().increment(key, -delta);
  166. }
  167. /**
  168. * HashGet
  169. *
  170. * @param key 键 不能为 null
  171. * @param item 项 不能为 null
  172. * @return 值
  173. */
  174. public Object hget(String key, String item) {
  175. return redisTemplate.opsForHash().get(key, item);
  176. }
  177. /**
  178. * 获取 hashKey对应的所有键值
  179. *
  180. * @param key 键
  181. * @return 对应的多个键值
  182. */
  183. public Map<Object, Object> hmget(String key) {
  184. return redisTemplate.opsForHash().entries(key);
  185. }
  186. /**
  187. * HashSet
  188. *
  189. * @param key 键
  190. * @param map 对应多个键值
  191. * @return true 成功 false 失败
  192. */
  193. public Boolean hmset(String key, Map<String, Object> map) {
  194. try {
  195. redisTemplate.opsForHash().putAll(key, map);
  196. return true;
  197. } catch (Exception e) {
  198. e.printStackTrace();
  199. return false;
  200. }
  201. }
  202. /**
  203. * HashSet 并设置时间
  204. *
  205. * @param key 键
  206. * @param map 对应多个键值
  207. * @param time 时间(秒)
  208. * @return true成功 false失败
  209. */
  210. public Boolean hmset(String key, Map<String, Object> map, Long time) {
  211. try {
  212. redisTemplate.opsForHash().putAll(key, map);
  213. if (time > 0) {
  214. expire(key, time);
  215. }
  216. return true;
  217. } catch (Exception e) {
  218. e.printStackTrace();
  219. return false;
  220. }
  221. }
  222. /**
  223. * 向一张hash表中放入数据,如果不存在将创建
  224. *
  225. * @param key 键
  226. * @param item 项
  227. * @param value 值
  228. * @return true 成功 false失败
  229. */
  230. public Boolean hset(String key, String item, Object value) {
  231. try {
  232. redisTemplate.opsForHash().put(key, item, value);
  233. return true;
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. return false;
  237. }
  238. }
  239. /**
  240. * 向一张hash表中放入数据,如果不存在将创建
  241. *
  242. * @param key 键
  243. * @param item 项
  244. * @param value 值
  245. * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
  246. * @return true 成功 false失败
  247. */
  248. public Boolean hset(String key, String item, Object value, Long time) {
  249. try {
  250. redisTemplate.opsForHash().put(key, item, value);
  251. if (time > 0) {
  252. expire(key, time);
  253. }
  254. return true;
  255. } catch (Exception e) {
  256. e.printStackTrace();
  257. return false;
  258. }
  259. }
  260. /**
  261. * 删除hash表中的值
  262. *
  263. * @param key 键 不能为 null
  264. * @param item 项 可以使多个不能为 null
  265. */
  266. public void hdel(String key, Object... item) {
  267. redisTemplate.opsForHash().delete(key, item);
  268. }
  269. /**
  270. * 判断hash表中是否有该项的值
  271. *
  272. * @param key 键 不能为 null
  273. * @param item 项 不能为 null
  274. * @return true 存在 false不存在
  275. */
  276. public Boolean hHasKey(String key, String item) {
  277. return redisTemplate.opsForHash().hasKey(key, item);
  278. }
  279. /**
  280. * hash递增 如果不存在,就会创建一个 并把新增后的值返回
  281. *
  282. * @param key 键
  283. * @param item 项
  284. * @param by 要增加几(大于0)
  285. * @return Double
  286. */
  287. public Double hincr(String key, String item, Double by) {
  288. return redisTemplate.opsForHash().increment(key, item, by);
  289. }
  290. /**
  291. * hash递减
  292. *
  293. * @param key 键
  294. * @param item 项
  295. * @param by 要减少记(小于0)
  296. * @return Double
  297. */
  298. public Double hdecr(String key, String item, Double by) {
  299. return redisTemplate.opsForHash().increment(key, item, -by);
  300. }
  301. /**
  302. * 根据 key获取 Set中的所有值
  303. *
  304. * @param key 键
  305. * @return Set
  306. */
  307. public Set<Object> sGet(String key) {
  308. try {
  309. return redisTemplate.opsForSet().members(key);
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. return null;
  313. }
  314. }
  315. /**
  316. * 根据value从一个set中查询,是否存在
  317. *
  318. * @param key 键
  319. * @param value 值
  320. * @return true 存在 false不存在
  321. */
  322. public Boolean sHasKey(String key, Object value) {
  323. try {
  324. return redisTemplate.opsForSet().isMember(key, value);
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. return false;
  328. }
  329. }
  330. /**
  331. * 将数据放入set缓存
  332. *
  333. * @param key 键
  334. * @param values 值 可以是多个
  335. * @return 成功个数
  336. */
  337. public Long sSet(String key, Object... values) {
  338. try {
  339. return redisTemplate.opsForSet().add(key, values);
  340. } catch (Exception e) {
  341. e.printStackTrace();
  342. return 0L;
  343. }
  344. }
  345. /**
  346. * 将set数据放入缓存
  347. *
  348. * @param key 键
  349. * @param time 时间(秒)
  350. * @param values 值 可以是多个
  351. * @return 成功个数
  352. */
  353. public Long sSetAndTime(String key, Long time, Object... values) {
  354. try {
  355. Long count = redisTemplate.opsForSet().add(key, values);
  356. if (time > 0) {
  357. expire(key, time);
  358. }
  359. return count;
  360. } catch (Exception e) {
  361. e.printStackTrace();
  362. return 0L;
  363. }
  364. }
  365. /**
  366. * 获取set缓存的长度
  367. *
  368. * @param key 键
  369. * @return Long
  370. */
  371. public Long sGetSetSize(String key) {
  372. try {
  373. return redisTemplate.opsForSet().size(key);
  374. } catch (Exception e) {
  375. e.printStackTrace();
  376. return 0L;
  377. }
  378. }
  379. /**
  380. * 移除值为value的
  381. *
  382. * @param key 键
  383. * @param values 值 可以是多个
  384. * @return 移除的个数
  385. */
  386. public Long setRemove(String key, Object... values) {
  387. try {
  388. return redisTemplate.opsForSet().remove(key, values);
  389. } catch (Exception e) {
  390. e.printStackTrace();
  391. return 0L;
  392. }
  393. }
  394. /**
  395. * 获取list缓存的内容
  396. *
  397. * @param key 键
  398. * @param start 开始
  399. * @param end 结束 0 到 -1代表所有值
  400. * @return List
  401. */
  402. public List<Object> lGet(String key, Long start, Long end) {
  403. try {
  404. return redisTemplate.opsForList().range(key, start, end);
  405. } catch (Exception e) {
  406. e.printStackTrace();
  407. return null;
  408. }
  409. }
  410. /**
  411. * 获取list缓存的长度
  412. *
  413. * @param key 键
  414. * @return Long
  415. */
  416. public Long lGetListSize(String key) {
  417. try {
  418. return redisTemplate.opsForList().size(key);
  419. } catch (Exception e) {
  420. e.printStackTrace();
  421. return 0L;
  422. }
  423. }
  424. /**
  425. * 通过索引 获取list中的值
  426. *
  427. * @param key 键
  428. * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;
  429. * index<0时,-1,表尾,-2倒数第二个元素,依次类推
  430. * @return Object
  431. */
  432. public Object lGetIndex(String key, Long index) {
  433. try {
  434. return redisTemplate.opsForList().index(key, index);
  435. } catch (Exception e) {
  436. e.printStackTrace();
  437. return null;
  438. }
  439. }
  440. /**
  441. * 将list放入缓存
  442. *
  443. * @param key 键
  444. * @param value 值
  445. * @return Boolean
  446. */
  447. public Boolean lSet(String key, Object value) {
  448. try {
  449. redisTemplate.opsForList().rightPush(key, value);
  450. return true;
  451. } catch (Exception e) {
  452. e.printStackTrace();
  453. return false;
  454. }
  455. }
  456. /**
  457. * 将list放入缓存
  458. *
  459. * @param key 键
  460. * @param value 值
  461. * @param time 时间(秒)
  462. * @return Boolean
  463. */
  464. public Boolean lSet(String key, Object value, Long time) {
  465. try {
  466. redisTemplate.opsForList().rightPush(key, value);
  467. if (time > 0) {
  468. expire(key, time);
  469. }
  470. return true;
  471. } catch (Exception e) {
  472. e.printStackTrace();
  473. return false;
  474. }
  475. }
  476. /**
  477. * 将list放入缓存
  478. *
  479. * @param key 键
  480. * @param value 值
  481. * @return Boolean
  482. */
  483. public Boolean lSet(String key, List<Object> value) {
  484. try {
  485. redisTemplate.opsForList().rightPushAll(key, value);
  486. return true;
  487. } catch (Exception e) {
  488. e.printStackTrace();
  489. return false;
  490. }
  491. }
  492. /**
  493. * 将list放入缓存
  494. *
  495. * @param key 键
  496. * @param value 值
  497. * @param time 时间(秒)
  498. * @return Boolean
  499. */
  500. public Boolean lSet(String key, List<Object> value, Long time) {
  501. try {
  502. redisTemplate.opsForList().rightPushAll(key, value);
  503. if (time > 0) {
  504. expire(key, time);
  505. }
  506. return true;
  507. } catch (Exception e) {
  508. e.printStackTrace();
  509. return false;
  510. }
  511. }
  512. /**
  513. * 根据索引修改list中的某条数据
  514. *
  515. * @param key 键
  516. * @param index 索引
  517. * @param value 值
  518. * @return Boolean
  519. */
  520. public Boolean lUpdateIndex(String key, Long index, Object value) {
  521. try {
  522. redisTemplate.opsForList().set(key, index, value);
  523. return true;
  524. } catch (Exception e) {
  525. e.printStackTrace();
  526. return false;
  527. }
  528. }
  529. /**
  530. * 移除N个值为value
  531. *
  532. * @param key 键
  533. * @param count 移除多少个
  534. * @param value 值
  535. * @return 移除的个数
  536. */
  537. public Long lRemove(String key, Long count, Object value) {
  538. try {
  539. return redisTemplate.opsForList().remove(key, count, value);
  540. } catch (Exception e) {
  541. e.printStackTrace();
  542. return 0L;
  543. }
  544. }
  545. public boolean setNx(String key, Object value, Long time) {
  546. if (time > 0) {
  547. return redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
  548. }
  549. return false;
  550. }
  551. public Set<String> keys(String name) {
  552. return redisTemplate.keys(name);
  553. }
  554. @AllArgsConstructor
  555. @Getter
  556. public enum key {
  557. CORP_AUTH_MAPPING_KEY("mapping:corp:", "企业微信授权跳转", 60 * 60 * 24L),
  558. CORP_ACCESSO_TOKEN_KEY("corp:access_token:", "corp_access_token", 60 * 30L),
  559. MAPPING_INDEX_KEY("mapper:index:", "授权跳转", 60 * 10L),
  560. WX_LOGIN_AUTH_URI("wx_login_auth_uri:%s:%s", "微信网页授权登录uri", 60 * 60 * 24L),
  561. RECOMMEND_COUPON_DAY("recommend_coupon_day:%s", "推荐优惠券弹窗", 60 * 60 * 24L),
  562. EMAIL_CODE_KEY("email_code_key:%s", "邮箱验证码", 60 * 3),
  563. PHONE_CODE_KEY("phone_code_key:", "短信验证码", 60 * 3),
  564. PHONE_CODE_USER_NUM_KEY_DAY("phone_code_user_num_key:", "近一天获取验证码次数", 60 * 60 * 24l),
  565. PHONE_CODE_BALCK_KEY("phone_code_balck_key:", "手机黑名单", -1l),
  566. LOTTERY_KEY("lottery_key:%s", "抽奖key", 60 * 60 * 24l),
  567. TASK_TIME_LIMIT_KEY("task_time_limit:%s", "一次性任务key", 30),
  568. WX_GZH_QRCODE_LOGIN("wx_gzh_qrcode_login:%s", "微信公众号扫码登录key", 60),
  569. WX_GZH_QRCODE_BIND("wx_gzh_qrcode_bind:%s", "pc手机用户购买车票绑定wx", 60),
  570. WX_GZH_QRCODE_SHARED_LOGIN("wx_gzh_qrcode_shared_login:%s", "微信公众号扫码分享邀请key", 60),
  571. EQUIPMENT_DISTRIBUTE_COUPON_KEY("equipment_distribute_coupon_key:%s", "实物设备分销新用户优惠券展示key", 60),
  572. CMS_USER_KEY("cms:user:%s:", "user缓存前缀", 60 * 60 * 24L * 7),
  573. CMS_USER_ROLE_KEY("cms:user:role:%s:", "user角色缓存前缀", 60 * 60 * 24L * 7),
  574. CMS_USER_PERMISSION_KEY("cms:user:permission:%s:", "user权限缓存前缀", 60 * 60 * 24L * 7),
  575. REPEAT_USER_KEY("repeat_user_key:%s:", "重复用户插入key", 60l),
  576. BUSINESS_FIRST_ORDER_USER_KEY("business_first_order_user_key:%s:", "商务分销首单提成key", 60l),
  577. PC_WEB_CACHE("pc_web_cache", "首页平台缓存", 10 * 60),
  578. H5_WEB_CACHE("h5_web_cache", "h5首页平台缓存", 10 * 60),
  579. YH_HOME_PAGHE_CACHE("yh_home_paghe_cache:", "银河首页缓存", 10 * 60),
  580. REGISTER_HOME_PAGE_CACHE("register_home_page_cache:", "注册首页缓存", 10 * 60),
  581. CPS_BACK_LOGIN_KEY("cps_back_login:", "cps商务后台登录key", 3 * 60),
  582. CPS_AUTH_SUCCESS_KEY("cps_auth_success_key:%s", "cps后台微信授权", 60 * 10),
  583. QUESTION_RESPONSE_KEY("question_response_key:%s", "问卷回答key", 60 * 60 * 24L),
  584. IMPORT_ACCOUNT_CACHE_KEY("import_account_cache_key:%s", "导入缓存key", 60 * 10L),
  585. MAKERTING_MSG_KEY("makerting_msg_key", "营销短信", 60 * 60 * 24 * 30),
  586. PHONE_AREA_CODE("phone_area_code:key", "国家或地区码", 60 * 60 * 24 * 15),
  587. CORP_CHAT_GROUP_CODE("corp_chat_group_code:", "企业微信群动态码", 60 * 60 * 24 * 15),
  588. CORP_SERVICE_QR_CODE("corp_service_qr_code:", "企业微信客服活码", 60 * 60 * 24 * 15),
  589. GENERAL_POPULARIZE_CODE("general_popularize_code:", "普通推广者关联优惠券", 10l),
  590. //企业微信欢迎语 临时素材 mediaId
  591. CORP_WECLOME_MEDIA("corp:%s:weclome:media:","临时素材 mediaId",60*60*24*3 - 60*60*1L),
  592. CORP_UPLOAD_ATTACHMENTS("corp:%s:upload:attachments:","临时附件",60*60*24*3 - 60*60*1L),
  593. CORP_DYNAMIC_RULE_TAG_KEY("corp_dynamic_rule_tag_key:", "企业微信动态标签key", 60 * 60L),
  594. ORDER_DON_POST_DATA_USER_KEY("order_don_post_data_user_key:", "用户订单回传缓存key", -1L),
  595. ABROAD_SUCCESS_URL("abroad:pay:success_url:","海外支付成功页",60 * 60 * 24L),
  596. CHAT_GPT_TOKEN_KEY("chat_gpt_token_key:", "chatGPT直连token key", 60 * 10l),
  597. CHAT_GPT_TOKEN_EXCEPTION_KEY("chat_gpt_token_exception_key:", "chatGPT直连token异常 key", -1l),
  598. ORDER_COMMENT_KEY("order_comment_key:", "订单评价", 10l),
  599. USER_BIND_ACCOUNT_INFO_KEY("user_bind_account_info_key:", "用户绑定其他账号key", 10l),
  600. TASK_RECEIVED_KEY("task_received_key:%s:%s", "领取任务奖励key", 10l),
  601. ;
  602. private String name;
  603. private String desc;
  604. private long timeout;
  605. public String getEnvName() {
  606. return this.name + env;
  607. }
  608. public String getNameFormat(Object ... str) {
  609. return String.format(this.getName(), str);
  610. }
  611. }
  612. }