RedisService.java 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. package com.webchat.common.service;
  2. import com.alibaba.nacos.common.utils.MapUtil;
  3. import com.webchat.common.exception.BusinessException;
  4. import com.webchat.common.util.JsonUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.collections.CollectionUtils;
  7. import org.apache.commons.lang3.ObjectUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.redis.core.*;
  11. import org.springframework.data.redis.core.script.DefaultRedisScript;
  12. import org.springframework.data.redis.serializer.RedisSerializer;
  13. import org.springframework.stereotype.Service;
  14. import redis.clients.jedis.Protocol;
  15. import redis.clients.jedis.util.SafeEncoder;
  16. import java.util.*;
  17. import java.util.concurrent.TimeUnit;
  18. import java.util.stream.Collectors;
  19. /**
  20. * redis 工具类
  21. */
  22. @Slf4j
  23. @Service
  24. public class RedisService {
  25. @Autowired
  26. private RedisTemplate<String, String> redisTemplate;
  27. /**
  28. * 发布消息
  29. *
  30. * @param channel
  31. * @param message
  32. */
  33. public void sendMessage(String channel, String message) {
  34. // redisTemplate.convertAndSend(channel, message);
  35. }
  36. /**
  37. * get取值
  38. *
  39. * @param key
  40. *
  41. * @return
  42. */
  43. public String get(final String key) {
  44. String result = null;
  45. try {
  46. ValueOperations<String, String> operations = redisTemplate.opsForValue();
  47. result = operations.get(key);
  48. } catch (Exception e) {
  49. log.error("redis get error! key:{}", key, e);
  50. }
  51. return result;
  52. }
  53. /***
  54. * 批量Get
  55. * @param keys
  56. *
  57. * [1,2,3]
  58. * [u1,null,u3]
  59. *
  60. * @return
  61. */
  62. public List<String> mget(final List<String> keys) {
  63. List<String> result = new ArrayList<>();
  64. try {
  65. ValueOperations<String, String> operations = this.redisTemplate.opsForValue();
  66. return operations.multiGet(keys);
  67. } catch (Exception e) {
  68. log.error("redis get error! key:{}", JsonUtil.toJsonString(keys), e);
  69. }
  70. return result;
  71. }
  72. public Map<String, String> mgetAndParseMap(final List<String> keys) {
  73. Map<String, String> resultMap = new HashMap<>(keys.size());
  74. try {
  75. List<String> result = this.mget(keys);
  76. for (int i = 0; i < keys.size(); i++) {
  77. resultMap.put(keys.get(i), result.get(i));
  78. }
  79. return resultMap;
  80. } catch (Exception e) {
  81. log.error("redis get error! key:{}", JsonUtil.toJsonString(keys), e);
  82. }
  83. return resultMap;
  84. }
  85. /***
  86. * 获取定时任务的锁, liveTime毫秒
  87. * @param key
  88. * @param requestId
  89. * @param liveTime
  90. * @return
  91. */
  92. public boolean installLockForMS(String key, String requestId, long liveTime) {
  93. if (org.springframework.util.StringUtils.isEmpty(key) || org.springframework.util.StringUtils.isEmpty(requestId)) {
  94. return false;
  95. }
  96. return setNxPx(key, requestId, liveTime);
  97. }
  98. /***
  99. * @param key
  100. * @param value
  101. * @param exptime
  102. * @return
  103. *
  104. * 1 ---> setnx(key, none , 2000) return true;
  105. * 2 ---> setnx return false;
  106. */
  107. public boolean setNxPx(final String key, final String value, final long exptime) {
  108. Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
  109. RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
  110. RedisSerializer keySerializer = redisTemplate.getKeySerializer();
  111. Object obj = connection.execute("set", keySerializer.serialize(key),
  112. valueSerializer.serialize(value),
  113. SafeEncoder.encode("NX"),
  114. SafeEncoder.encode("PX"),
  115. Protocol.toByteArray(exptime));
  116. return obj != null && "OK".equals(new String((byte[]) obj));
  117. });
  118. return result;
  119. }
  120. /**
  121. * @param key
  122. * @param value
  123. * @param exptime
  124. *
  125. * @return
  126. */
  127. public boolean setNxEx(final String key, final String value, final long exptime) {
  128. if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
  129. return false;
  130. }
  131. Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
  132. RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
  133. RedisSerializer keySerializer = redisTemplate.getKeySerializer();
  134. Object obj = connection.execute("set", keySerializer.serialize(key),
  135. valueSerializer.serialize(value),
  136. SafeEncoder.encode("NX"),
  137. SafeEncoder.encode("EX"),
  138. Protocol.toByteArray(exptime));
  139. return ObjectUtils.equals("OK", obj);
  140. });
  141. return result;
  142. }
  143. /**
  144. * 带有过期时间的set传值
  145. *
  146. * @param key
  147. * @param value
  148. *
  149. * @return
  150. */
  151. public boolean set(final String key, final String value, long expireTime) {
  152. boolean result = false;
  153. try {
  154. ValueOperations<String, String> operations = redisTemplate.opsForValue();
  155. if (expireTime > 0) {
  156. operations.set(key, value, expireTime, TimeUnit.SECONDS);
  157. } else {
  158. operations.set(key, value);
  159. }
  160. result = true;
  161. } catch (Exception e) {
  162. log.error("redis set error! key:{}, value:{}", key, value, e);
  163. }
  164. return result;
  165. }
  166. /**
  167. * set传值
  168. *
  169. * @param key
  170. * @param value
  171. *
  172. * @return
  173. */
  174. public boolean set(final String key, String value) {
  175. boolean result = false;
  176. try {
  177. ValueOperations<String, String> operations = redisTemplate.opsForValue();
  178. operations.set(key, value);
  179. result = true;
  180. } catch (Exception e) {
  181. log.error("redis set error! key:{}, value:{}", key, value);
  182. }
  183. return result;
  184. }
  185. /**
  186. * @param key
  187. * @param value
  188. *
  189. * @return
  190. */
  191. public boolean sadd(String key, String... value) {
  192. boolean result = false;
  193. try {
  194. SetOperations<String, String> set = redisTemplate.opsForSet();
  195. set.add(key, value);
  196. result = true;
  197. } catch (Exception e) {
  198. log.error("redis set error! key:{}, value:{}", key, value, e);
  199. }
  200. return result;
  201. }
  202. /**
  203. * @param key
  204. * @param setValue
  205. */
  206. public void sadd(String key, Set<String> setValue) {
  207. try {
  208. SetOperations<String, String> set = redisTemplate.opsForSet();
  209. set.add(key, setValue.toArray(new String[0]));
  210. } catch (Exception e) {
  211. log.error("sadd error! key:{}", key, e);
  212. }
  213. }
  214. /**
  215. * @param key
  216. *
  217. * @return
  218. */
  219. public Set<String> smembers(String key) {
  220. try {
  221. SetOperations<String, String> set = redisTemplate.opsForSet();
  222. return set.members(key);
  223. } catch (Exception e) {
  224. log.error("redis set error! key:{}", key, e);
  225. return Collections.emptySet();
  226. }
  227. }
  228. public Long ssize(String key) {
  229. Long size = null;
  230. try {
  231. size = redisTemplate.opsForSet().size(key);
  232. } catch (Exception e) {
  233. log.error("redis ssize error! key:{}", key, e);
  234. }
  235. return size;
  236. }
  237. /**
  238. * @param key
  239. * @param values
  240. *
  241. * @return
  242. */
  243. public Long sremove(String key, Object... values) {
  244. try {
  245. SetOperations<String, String> set = redisTemplate.opsForSet();
  246. return set.remove(key, values);
  247. } catch (Exception e) {
  248. log.error("redis set error! key:{}", key, e);
  249. return null;
  250. }
  251. }
  252. /**
  253. * 元素是否在set集合中
  254. * @param key
  255. * @param value
  256. * @return
  257. */
  258. public Boolean sIsMember(String key, String value) {
  259. try {
  260. SetOperations<String, String> set = redisTemplate.opsForSet();
  261. return set.isMember(key, value);
  262. } catch (Exception ex) {
  263. log.error("redis sIsMember error! key:{}", key, ex);
  264. return false;
  265. }
  266. }
  267. /**
  268. * @param key
  269. * @param value
  270. * @param score
  271. *
  272. * @return
  273. */
  274. public boolean zadd(String key, String value, long score) {
  275. boolean result = false;
  276. try {
  277. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  278. zset.add(key, value, score);
  279. result = true;
  280. } catch (Exception e) {
  281. log.error("redis set error! key:{}, value:{}", key, value, e);
  282. }
  283. return result;
  284. }
  285. /***
  286. * 判断zset中是否包含元素
  287. * @param key
  288. * @param value
  289. * @return
  290. */
  291. public boolean zContains(String key, String value) {
  292. try {
  293. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  294. Long rank = zset.rank(key, value);
  295. return rank != null && rank >= 0;
  296. } catch (Exception e) {
  297. log.error("redis zContains error! key:{}, value:{}", key, value, e);
  298. }
  299. return false;
  300. }
  301. /**
  302. * @param key
  303. * @param value
  304. * @param score
  305. * @param liveTime
  306. *
  307. * @return
  308. */
  309. public boolean zadd(String key, String value, long score, long liveTime) {
  310. boolean result = false;
  311. try {
  312. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  313. zset.add(key, value, score);
  314. if (liveTime > 0) {
  315. expire(key, liveTime);
  316. }
  317. result = true;
  318. } catch (Exception e) {
  319. log.error("redis set error! key:{}, value:{}", key, value, e);
  320. }
  321. return result;
  322. }
  323. /**
  324. * @param key
  325. * @param tuples
  326. * @param liveTime
  327. *
  328. * @return
  329. */
  330. public boolean zadd(String key, Set<ZSetOperations.TypedTuple<String>> tuples, long liveTime) {
  331. boolean result = false;
  332. try {
  333. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  334. zset.add(key, tuples);
  335. if (liveTime > 0) {
  336. expire(key, liveTime);
  337. }
  338. result = true;
  339. } catch (Exception e) {
  340. log.error("redis set error! key:{}, value:{}", key, tuples, e);
  341. }
  342. return result;
  343. }
  344. /**
  345. * @param key
  346. * @param start
  347. * @param end
  348. * @return
  349. */
  350. public Set<String> zrange(String key, long start, long end) {
  351. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  352. try {
  353. Set<String> result = zset.range(key, start, end - 1L);
  354. return result;
  355. } catch (Exception e) {
  356. log.error("redis zrange error,key:{}", key, e);
  357. }
  358. return null;
  359. }
  360. /***
  361. * 获取Score
  362. * @param key
  363. * @param value
  364. * @return
  365. */
  366. public Long zscore(String key, String value) {
  367. Double score = redisTemplate.opsForZSet().score(key, value);
  368. if (score == null) {
  369. return 0L;
  370. }
  371. return score.longValue();
  372. }
  373. /**
  374. * 批量查询zset score评分
  375. *
  376. * @param key
  377. * @param values
  378. * @return
  379. */
  380. public List<Long> zscore(String key, List<String> values) {
  381. if (StringUtils.isBlank(key) || CollectionUtils.isEmpty(values)) {
  382. return new ArrayList<>();
  383. }
  384. List<Double> scores = redisTemplate.opsForZSet().score(key, values.toArray(new String[values.size()]));
  385. if (CollectionUtils.isEmpty(scores)) {
  386. return new ArrayList<>();
  387. }
  388. return scores.stream().map(Double::longValue).collect(Collectors.toList());
  389. }
  390. /**
  391. * 批量查询redis zset value对应score评分
  392. *
  393. * @param key
  394. * @param values
  395. * @return
  396. */
  397. public Map<String, Long> zscoreTomap(String key, List<String> values) {
  398. List<Long> scores = this.zscore(key, values);
  399. if (CollectionUtils.isEmpty(scores)) {
  400. return new HashMap<>();
  401. }
  402. Map<String, Long> scoreMap = new HashMap<>();
  403. for (int i = 0; i < values.size(); i++) {
  404. scoreMap.put(values.get(i), scores.get(i));
  405. }
  406. return scoreMap;
  407. }
  408. /**
  409. * @param key
  410. * @param start
  411. * @param end
  412. *
  413. * @return
  414. */
  415. public Set<String> zreverseRange(String key, long start, long end) {
  416. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  417. try {
  418. Set<String> result = zset.reverseRange(key, start, end - 1L);
  419. return result;
  420. } catch (Exception e) {
  421. log.error("redis zrange error,errorMessage:{}", e.getMessage());
  422. }
  423. return new HashSet<>();
  424. }
  425. /**
  426. * 获取ZSet分数区间中前几位的元素
  427. * @param key
  428. * @param maxScore
  429. * @param offset
  430. * @param count
  431. * @return
  432. */
  433. public Set<String> zreverseRangeByScore(String key, double maxScore, long offset, long count) {
  434. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  435. try {
  436. Set<String> result = zset.reverseRangeByScore(key, 1 - Double.MAX_VALUE, maxScore, offset, count);
  437. return result;
  438. } catch (Exception e) {
  439. log.error("redis zrangeByScoreWithScores error,errorMessage:{}",
  440. e.getMessage());
  441. }
  442. return null;
  443. }
  444. /**
  445. * 获取ZSet分数区间中前几位的元素
  446. * @param key
  447. * @param minScore
  448. * @param maxScore
  449. * @param offset
  450. * @param count
  451. * @return
  452. */
  453. public Set<String> zreverseRangeByScore(String key, double minScore, double maxScore, long offset, long count) {
  454. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  455. try {
  456. Set<String> result = zset.reverseRangeByScore(key, minScore, maxScore, offset, count);
  457. return result;
  458. } catch (Exception e) {
  459. log.error("redis zrangeByScoreWithScores error,errorMessage:{}",
  460. e.getMessage());
  461. }
  462. return null;
  463. }
  464. /**
  465. * 获取ZSet中所有元素总数
  466. * @param key
  467. */
  468. public long zSetSize(String key) {
  469. if (StringUtils.isEmpty(key)) {
  470. return 0;
  471. }
  472. try {
  473. return redisTemplate.opsForZSet().size(key);
  474. } catch (Exception ex) {
  475. log.error("zSetSize error", ex);
  476. return 0;
  477. }
  478. }
  479. /**
  480. * 获取ZSet中所有分数区间元素总数
  481. * @param key
  482. */
  483. public long zSetCount(String key, double minScore, double maxScore) {
  484. if (StringUtils.isEmpty(key)) {
  485. return 0;
  486. }
  487. try {
  488. return redisTemplate.opsForZSet().count(key, minScore, maxScore);
  489. } catch (Exception ex) {
  490. log.error("zSetCount error", ex);
  491. return 0;
  492. }
  493. }
  494. /**
  495. * 获取ZSet中所有元素
  496. * @param key
  497. */
  498. public Set<String> zSetGetAll(String key) {
  499. if (StringUtils.isEmpty(key)) {
  500. return null;
  501. }
  502. try {
  503. return redisTemplate.opsForZSet().reverseRange(key, 0, -1);
  504. } catch (Exception ex) {
  505. log.error("zSetGetAll error", ex);
  506. return null;
  507. }
  508. }
  509. /**
  510. * @param key
  511. * @param start
  512. * @param end
  513. *
  514. * @return
  515. */
  516. public Set<String> zrangeByScore(String key, long start, long end) {
  517. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  518. try {
  519. Set<String> result = zset.rangeByScore(key, start, end);
  520. return result;
  521. } catch (Exception e) {
  522. log.error("redis zrangeByScore error,errorMessage:{}", e.getMessage());
  523. }
  524. return null;
  525. }
  526. /**
  527. * 按照分数范围删除
  528. *
  529. * @param key
  530. * @param start
  531. * @param end
  532. */
  533. public void zremoveRangeByScore(String key, long start, long end) {
  534. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  535. try {
  536. zset.removeRangeByScore(key, start, end);
  537. } catch (Exception e) {
  538. log.error("redis zremoveRangeByScore error,errorMessage:{}", e.getMessage());
  539. }
  540. }
  541. /**
  542. * 删除ZSet中某一元素
  543. * @param key
  544. * @param value
  545. */
  546. public void zremove(String key, String value) {
  547. if (StringUtils.isEmpty(key)) {
  548. return;
  549. }
  550. try {
  551. redisTemplate.opsForZSet().remove(key, value);
  552. } catch (Exception ex) {
  553. log.error("zSetAdd error", ex);
  554. }
  555. }
  556. /**
  557. * Zset取交集
  558. * {key} 与 {otherKey} 交集赋值给 {destKey}
  559. */
  560. public void zintersectAndStore(String key, String otherKey, String destKey) {
  561. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(otherKey) || StringUtils.isEmpty(destKey)) {
  562. return;
  563. }
  564. try {
  565. redisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey);
  566. } catch (Exception ex) {
  567. log.error("zSetAdd error", ex);
  568. }
  569. }
  570. /**
  571. * Zset取交集(使用lua的方式)
  572. * {key} 与 {otherKey} 交集赋值给 {destKey},分数取最小的值
  573. * @param key
  574. * @param otherKey
  575. * @param destKey
  576. * @return
  577. */
  578. public Long zinterstoreMin(String key, String otherKey, String destKey) {
  579. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(otherKey) || StringUtils.isEmpty(destKey)) {
  580. return null;
  581. }
  582. List<String> keys = Arrays.asList(destKey, key, otherKey);
  583. String script = " return redis.call('zinterstore', KEYS[1], '2', KEYS[2], KEYS[3], 'aggregate', 'min') ";
  584. DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
  585. try {
  586. Object res = redisTemplate.execute(redisScript, keys);
  587. return (Long) res;
  588. } catch (Exception ex) {
  589. log.error("zinterstoreMin error, key:{}, otherKey:{}, destKey:{}", key, otherKey, destKey, ex);
  590. }
  591. return null;
  592. }
  593. /**
  594. * zset复制数据
  595. * 将{key}的数据复制到{destKey}上
  596. * @param key
  597. * @param destKey
  598. * @return
  599. */
  600. public Long zunionstore(String key, String destKey) {
  601. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(destKey)) {
  602. return null;
  603. }
  604. List<String> keys = Arrays.asList(destKey, key);
  605. String script = " return redis.call('zunionstore', KEYS[1], '1', KEYS[2]) ";
  606. DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
  607. try {
  608. Object res = redisTemplate.execute(redisScript, keys);
  609. return (Long) res;
  610. } catch (Exception ex) {
  611. log.error("zunionstore error, key:{}, destKey:{}", key, destKey, ex);
  612. }
  613. return null;
  614. }
  615. /**
  616. * @param key
  617. *
  618. * @return
  619. */
  620. public Long zsize(String key) {
  621. ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
  622. try {
  623. Long result = zset.size(key);
  624. return result;
  625. } catch (Exception e) {
  626. log.error("redis zrange error,errorMessage:{}", e.getMessage());
  627. }
  628. return null;
  629. }
  630. /**
  631. * hset
  632. *
  633. * @param key
  634. * @param field
  635. * @param value
  636. *
  637. * @return
  638. */
  639. public boolean hset(String key, String field, String value, long... expire) {
  640. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field) || StringUtils.isEmpty(value)) {
  641. return false;
  642. }
  643. boolean result = false;
  644. try {
  645. final byte[] rawKey = redisTemplate.getStringSerializer().serialize(key);
  646. final byte[] rawField = redisTemplate.getStringSerializer().serialize(field);
  647. final byte[] rawValue = redisTemplate.getStringSerializer().serialize(value);
  648. result = redisTemplate.execute(connection -> {
  649. boolean ret = connection.hSet(rawKey, rawField, rawValue);
  650. if (expire.length > 0 && expire[0] > 0) {
  651. connection.expire(rawKey, expire[0]);
  652. }
  653. return ret;
  654. }, true);
  655. } catch (Exception ex) {
  656. log.error("hset error", ex);
  657. }
  658. return result;
  659. }
  660. /**
  661. * hget
  662. *
  663. * @param key
  664. * @param field
  665. *
  666. * @return
  667. */
  668. public String hget(String key, String field) {
  669. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
  670. return null;
  671. }
  672. final byte[] rawKey = redisTemplate.getStringSerializer().serialize(key);
  673. final byte[] rawField = redisTemplate.getStringSerializer().serialize(field);
  674. final byte[] rawValue = redisTemplate.execute(connection -> connection.hGet(rawKey, rawField), true);
  675. return redisTemplate.getStringSerializer().deserialize(rawValue);
  676. }
  677. /**
  678. * @param key
  679. * @param field
  680. *
  681. * @return
  682. */
  683. public Long hdel(String key, String field) {
  684. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
  685. return null;
  686. }
  687. long result = redisTemplate.opsForHash().delete(key, field);
  688. return result;
  689. }
  690. /**
  691. * @param key
  692. * @param queryFields
  693. *
  694. * @return
  695. */
  696. public List<String> hmget(String key, List<String> queryFields) {
  697. HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
  698. return hashOperations.multiGet(key, queryFields);
  699. }
  700. /**
  701. * @param key
  702. * @param map
  703. *
  704. * @return
  705. */
  706. public void hmSet(String key, Map<Long, Long> map) {
  707. if (StringUtils.isEmpty(key) || MapUtil.isEmpty(map)) {
  708. return;
  709. }
  710. redisTemplate.opsForHash().putAll(key, map);
  711. }
  712. /**
  713. * @param key
  714. * @param map
  715. */
  716. public void hmSet(String key, Map<String, String> map, long... expire) {
  717. if (StringUtils.isEmpty(key) || MapUtil.isEmpty(map)) {
  718. return;
  719. }
  720. redisTemplate.opsForHash().putAll(key, map);
  721. }
  722. /**
  723. * @param key
  724. * @param field
  725. *
  726. * @return
  727. */
  728. public Long hincrex(String key, String field) {
  729. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
  730. return null;
  731. }
  732. Long result = redisTemplate.opsForHash().increment(key, field, 1);
  733. return result;
  734. }
  735. public Long hIncrementVal(String key, String field, long val) {
  736. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
  737. return null;
  738. }
  739. Long result = redisTemplate.opsForHash().increment(key, field, val);
  740. return result;
  741. }
  742. /**
  743. * @param key
  744. * @param field
  745. *
  746. * @return
  747. */
  748. public Long hdecrex(String key, String field) {
  749. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
  750. return null;
  751. }
  752. Long result = redisTemplate.opsForHash().increment(key, field, -1);
  753. return result;
  754. }
  755. public void lleftPush(String key, String value) {
  756. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
  757. return;
  758. }
  759. redisTemplate.opsForList().leftPush(key, value);
  760. }
  761. public void lleftPushAll(String key, Collection values) {
  762. if (StringUtils.isEmpty(key) || CollectionUtils.isEmpty(values)) {
  763. return;
  764. }
  765. redisTemplate.opsForList().leftPushAll(key, values);
  766. }
  767. public void lrightPush(String key, String value) {
  768. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
  769. return;
  770. }
  771. redisTemplate.opsForList().rightPush(key, value);
  772. }
  773. public void lrightPushAll(String key, List<String> valueList) {
  774. if (StringUtils.isEmpty(key) || CollectionUtils.isEmpty(valueList)) {
  775. return;
  776. }
  777. redisTemplate.opsForList().rightPushAll(key, valueList);
  778. }
  779. public void lleftPushAll(String key, List<String> valueList) {
  780. if (StringUtils.isEmpty(key) || CollectionUtils.isEmpty(valueList)) {
  781. return;
  782. }
  783. redisTemplate.opsForList().leftPushAll(key, valueList);
  784. }
  785. public Long lsize(String key) {
  786. if (StringUtils.isEmpty(key)) {
  787. return null;
  788. }
  789. return redisTemplate.opsForList().size(key);
  790. }
  791. /**
  792. * @param key
  793. * @param start
  794. * @param end
  795. */
  796. public List<String> lrange(String key, long start, long end) {
  797. if (StringUtils.isEmpty(key)) {
  798. return null;
  799. }
  800. return redisTemplate.opsForList().range(key, start, end);
  801. }
  802. /**
  803. * @param key
  804. *
  805. * @return
  806. */
  807. public String lleftPop(String key) {
  808. if (StringUtils.isEmpty(key)) {
  809. return null;
  810. }
  811. return redisTemplate.opsForList().leftPop(key);
  812. }
  813. /**
  814. * @param key
  815. * @param time
  816. * @param timeUnit
  817. * @return
  818. */
  819. public String lleftPop(String key, long time, TimeUnit timeUnit) {
  820. if (StringUtils.isEmpty(key)) {
  821. return null;
  822. }
  823. try {
  824. return redisTemplate.opsForList().leftPop(key, time, timeUnit);
  825. } catch (Exception e) {
  826. log.error("redis lleftPop error! key:{}", key, e);
  827. }
  828. return null;
  829. }
  830. /**
  831. * @param key
  832. * @param time
  833. * @param timeUnit
  834. * @return
  835. */
  836. public String lrightPop(String key, long time, TimeUnit timeUnit) {
  837. if (StringUtils.isEmpty(key)) {
  838. return null;
  839. }
  840. try {
  841. return redisTemplate.opsForList().rightPop(key, time, timeUnit);
  842. } catch (Exception e) {
  843. log.error("redis lrightPop error! key:{}", key, e);
  844. }
  845. return null;
  846. }
  847. /**
  848. * 带失效时间的值原子自增
  849. *
  850. * @param key
  851. * @param sec
  852. *
  853. * @return
  854. */
  855. public Long increx(String key, long sec) {
  856. Long result = null;
  857. try {
  858. ValueOperations<String, String> operations = redisTemplate.opsForValue();
  859. result = operations.increment(key, 1);
  860. // 第一次设置,设置有效时间
  861. if (result == 1) {
  862. expire(key, sec);
  863. }
  864. } catch (Exception e) {
  865. log.error("redis get error! key:{}", key, e);
  866. }
  867. return result;
  868. }
  869. /**
  870. * 带失效时间的值原子自减
  871. *
  872. * @param key
  873. * @param sec
  874. *
  875. * @return
  876. */
  877. public Long decrex(String key, long sec) {
  878. Long result = null;
  879. try {
  880. ValueOperations<String, String> operations = redisTemplate.opsForValue();
  881. result = operations.increment(key, -1);
  882. // 第一次设置,设置有效时间
  883. if (result == -1) {
  884. expire(key, sec);
  885. }
  886. } catch (Exception e) {
  887. log.error("redis get error! key:{}", key, e);
  888. }
  889. return result;
  890. }
  891. /**
  892. * 获取定时任务的锁(非集群方式)
  893. *
  894. * @param key 键值
  895. * @param requestId 锁ID,通过此来判断是哪个实例的锁
  896. * @param liveTime 过期时间
  897. *
  898. * @return
  899. */
  900. public boolean installDistributedLock(String key, String requestId, long liveTime) {
  901. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(requestId)) {
  902. return false;
  903. }
  904. return setNxEx(key, requestId, liveTime);
  905. }
  906. /**
  907. * 释放定时任务的锁
  908. *
  909. * @param key 键值
  910. * @param requestId 锁ID
  911. */
  912. public void releaseDistributedLock(String key, String requestId) {
  913. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(requestId)) {
  914. return;
  915. }
  916. try {
  917. String redisId = redisTemplate.opsForValue().get(key);
  918. // 保证只解自己拿到的锁
  919. if (requestId.equals(redisId)) {
  920. redisTemplate.delete(key);
  921. }
  922. } catch (Exception e) {
  923. log.error("release distributed lock error,requestId:{},errorMessage:{}",
  924. requestId, e.getMessage());
  925. throw new BusinessException("release distributed lock error");
  926. }
  927. }
  928. /**
  929. * 删除对应的value
  930. *
  931. * @param key
  932. */
  933. public void remove(final String key) {
  934. redisTemplate.delete(key);
  935. }
  936. /**
  937. * 判断缓存中是否有对应的value
  938. *
  939. * @param key
  940. *
  941. * @return
  942. */
  943. public Boolean exists(final String key) {
  944. return redisTemplate.hasKey(key);
  945. }
  946. /**
  947. * 设置过期时间
  948. *
  949. * @param key
  950. * @param livetime
  951. */
  952. public void expire(String key, long livetime) {
  953. redisTemplate.expire(key, livetime, TimeUnit.SECONDS);
  954. }
  955. /***
  956. * 求两个set的交集
  957. */
  958. public Set<String> intersect(String key1, String key2) {
  959. try {
  960. SetOperations<String, String> set = redisTemplate.opsForSet();
  961. return set.intersect(key1, key2);
  962. } catch (Exception e) {
  963. log.error("redis intersect error! key1:{}, key2:{}", key1, key2, e);
  964. return Collections.emptySet();
  965. }
  966. }
  967. /***
  968. * 求两个set的并集
  969. */
  970. public Set<String> union(String key1, String key2) {
  971. try {
  972. SetOperations<String, String> set = redisTemplate.opsForSet();
  973. return set.union(key1, key2);
  974. } catch (Exception e) {
  975. log.error("redis union error! key1:{}, key2:{}", key1, key2, e);
  976. return Collections.emptySet();
  977. }
  978. }
  979. public Long size(String key) {
  980. SetOperations<String, String> set = redisTemplate.opsForSet();
  981. try {
  982. Long result = set.size(key);
  983. return result;
  984. } catch (Exception e) {
  985. log.error("redis size error, key:{}, errorMessage:{}",key, e.getMessage());
  986. }
  987. return null;
  988. }
  989. /***
  990. * 查询剩余存活时间
  991. * @param key
  992. * @return
  993. */
  994. public Long getKeysExpireTime(String key) {
  995. Set<String> keys = redisTemplate.keys(key);
  996. return redisTemplate.opsForValue().getOperations().getExpire(key);
  997. }
  998. /***
  999. * 根据前缀查询所有key的剩余存活时间
  1000. * @return
  1001. */
  1002. public Map<String, Long> batchGetKeysExpireTime(String keyPrefix) {
  1003. Set<String> keys = redisTemplate.keys(keyPrefix + "*");
  1004. Map<String, Long> keyExpireMap = new HashMap<>();
  1005. for (String key : keys) {
  1006. Long expireTime = redisTemplate.opsForValue().getOperations().getExpire(key);
  1007. keyExpireMap.put(key, expireTime);
  1008. }
  1009. return keyExpireMap;
  1010. }
  1011. /**
  1012. * @param start
  1013. * @param end
  1014. * @return
  1015. */
  1016. public Map<Long, Set<DefaultTypedTuple>> zreverseRangeWithScoreByPipelined(List<String> keys,
  1017. List<Long> indexs,
  1018. long start, long end) {
  1019. if (CollectionUtils.isEmpty(keys)) {
  1020. return new HashMap<>();
  1021. }
  1022. List<Object> redisResult = redisTemplate.executePipelined((RedisCallback<List<Object>>) connection -> {
  1023. for (String key : keys) {
  1024. byte[] rewKey = redisTemplate.getStringSerializer().serialize(key);
  1025. connection.zRevRangeWithScores(rewKey, start, end - 1);
  1026. }
  1027. return null;
  1028. });
  1029. Map<Long, Set<DefaultTypedTuple>> resultMap = new HashMap<>();
  1030. if (!CollectionUtils.isEmpty(redisResult)) {
  1031. for (int i = 0; i < indexs.size(); i++) {
  1032. Set<DefaultTypedTuple> result = (Set<DefaultTypedTuple>) redisResult.get(i);
  1033. resultMap.put(indexs.get(i), result);
  1034. }
  1035. }
  1036. return resultMap;
  1037. }
  1038. }