RedisService.java 19 KB

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