Springboot整合异步定时任务

我不是女神ヾ 2022-04-12 04:17 481阅读 0赞

一、开启异步与定时任务

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.scheduling.annotation.EnableAsync;
  6. import org.springframework.scheduling.annotation.EnableScheduling;
  7. /**
  8. * 启动控制类
  9. * 开启缓存
  10. * @author nick
  11. */
  12. @SpringBootApplication
  13. @EnableConfigurationProperties
  14. @EnableAsync //开启异步注解功能
  15. @EnableScheduling //开启基于注解的定时任务
  16. public class Application {
  17. public static void main(String[] args) {
  18. SpringApplication.run(Application.class, args);
  19. }
  20. }

二、在@Service层进行异步与定时操作

  1. import org.springframework.scheduling.annotation.Async;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class ExportExcelService {
  6. /**
  7. * Spring这是一个异步方法
  8. */
  9. @Async
  10. @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次
  11. public void hello(){
  12. System.out.println("处理数据中...");
  13. System.err.println("========="+System.currentTimeMillis());
  14. }
  15. }

三、推荐一个redisUtils工具类很全

  1. package com.citydo.utils;
  2. import org.springframework.data.redis.connection.DataType;
  3. import org.springframework.data.redis.core.Cursor;
  4. import org.springframework.data.redis.core.ScanOptions;
  5. import org.springframework.data.redis.core.StringRedisTemplate;
  6. import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
  7. import java.util.Collection;
  8. import java.util.Date;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Map.Entry;
  12. import java.util.Set;
  13. import java.util.concurrent.TimeUnit;
  14. /**
  15. * Redis工具类
  16. * @author WangFan
  17. * @date 2018-02-24 下午03:09:50
  18. * @version 1.1 (GitHub文档: https://github.com/whvcse/RedisUtil )
  19. */
  20. public class RedisUtil {
  21. private StringRedisTemplate redisTemplate;
  22. public void setRedisTemplate(StringRedisTemplate redisTemplate) {
  23. this.redisTemplate = redisTemplate;
  24. }
  25. public StringRedisTemplate getRedisTemplate() {
  26. return this.redisTemplate;
  27. }
  28. /** -------------------key相关操作--------------------- */
  29. /**
  30. * 删除key
  31. *
  32. * @param key
  33. */
  34. public void delete(String key) {
  35. redisTemplate.delete(key);
  36. }
  37. /**
  38. * 批量删除key
  39. *
  40. * @param keys
  41. */
  42. public void delete(Collection<String> keys) {
  43. redisTemplate.delete(keys);
  44. }
  45. /**
  46. * 序列化key
  47. *
  48. * @param key
  49. * @return
  50. */
  51. public byte[] dump(String key) {
  52. return redisTemplate.dump(key);
  53. }
  54. /**
  55. * 是否存在key
  56. *
  57. * @param key
  58. * @return
  59. */
  60. public Boolean hasKey(String key) {
  61. return redisTemplate.hasKey(key);
  62. }
  63. /**
  64. * 设置过期时间
  65. *
  66. * @param key
  67. * @param timeout
  68. * @param unit
  69. * @return
  70. */
  71. public Boolean expire(String key, long timeout, TimeUnit unit) {
  72. return redisTemplate.expire(key, timeout, unit);
  73. }
  74. /**
  75. * 设置过期时间
  76. *
  77. * @param key
  78. * @param date
  79. * @return
  80. */
  81. public Boolean expireAt(String key, Date date) {
  82. return redisTemplate.expireAt(key, date);
  83. }
  84. /**
  85. * 查找匹配的key
  86. *
  87. * @param pattern
  88. * @return
  89. */
  90. public Set<String> keys(String pattern) {
  91. return redisTemplate.keys(pattern);
  92. }
  93. /**
  94. * 将当前数据库的 key 移动到给定的数据库 db 当中
  95. *
  96. * @param key
  97. * @param dbIndex
  98. * @return
  99. */
  100. public Boolean move(String key, int dbIndex) {
  101. return redisTemplate.move(key, dbIndex);
  102. }
  103. /**
  104. * 移除 key 的过期时间,key 将持久保持
  105. *
  106. * @param key
  107. * @return
  108. */
  109. public Boolean persist(String key) {
  110. return redisTemplate.persist(key);
  111. }
  112. /**
  113. * 返回 key 的剩余的过期时间
  114. *
  115. * @param key
  116. * @param unit
  117. * @return
  118. */
  119. public Long getExpire(String key, TimeUnit unit) {
  120. return redisTemplate.getExpire(key, unit);
  121. }
  122. /**
  123. * 返回 key 的剩余的过期时间
  124. *
  125. * @param key
  126. * @return
  127. */
  128. public Long getExpire(String key) {
  129. return redisTemplate.getExpire(key);
  130. }
  131. /**
  132. * 从当前数据库中随机返回一个 key
  133. *
  134. * @return
  135. */
  136. public String randomKey() {
  137. return redisTemplate.randomKey();
  138. }
  139. /**
  140. * 修改 key 的名称
  141. *
  142. * @param oldKey
  143. * @param newKey
  144. */
  145. public void rename(String oldKey, String newKey) {
  146. redisTemplate.rename(oldKey, newKey);
  147. }
  148. /**
  149. * 仅当 newkey 不存在时,将 oldKey 改名为 newkey
  150. *
  151. * @param oldKey
  152. * @param newKey
  153. * @return
  154. */
  155. public Boolean renameIfAbsent(String oldKey, String newKey) {
  156. return redisTemplate.renameIfAbsent(oldKey, newKey);
  157. }
  158. /**
  159. * 返回 key 所储存的值的类型
  160. *
  161. * @param key
  162. * @return
  163. */
  164. public DataType type(String key) {
  165. return redisTemplate.type(key);
  166. }
  167. /** -------------------string相关操作--------------------- */
  168. /**
  169. * 设置指定 key 的值
  170. * @param key
  171. * @param value
  172. */
  173. public void set(String key, String value) {
  174. redisTemplate.opsForValue().set(key, value);
  175. }
  176. /**
  177. * 获取指定 key 的值
  178. * @param key
  179. * @return
  180. */
  181. public String get(String key) {
  182. return redisTemplate.opsForValue().get(key);
  183. }
  184. /**
  185. * 返回 key 中字符串值的子字符
  186. * @param key
  187. * @param start
  188. * @param end
  189. * @return
  190. */
  191. public String getRange(String key, long start, long end) {
  192. return redisTemplate.opsForValue().get(key, start, end);
  193. }
  194. /**
  195. * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
  196. *
  197. * @param key
  198. * @param value
  199. * @return
  200. */
  201. public String getAndSet(String key, String value) {
  202. return redisTemplate.opsForValue().getAndSet(key, value);
  203. }
  204. /**
  205. * 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
  206. *
  207. * @param key
  208. * @param offset
  209. * @return
  210. */
  211. public Boolean getBit(String key, long offset) {
  212. return redisTemplate.opsForValue().getBit(key, offset);
  213. }
  214. /**
  215. * 批量获取
  216. *
  217. * @param keys
  218. * @return
  219. */
  220. public List<String> multiGet(Collection<String> keys) {
  221. return redisTemplate.opsForValue().multiGet(keys);
  222. }
  223. /**
  224. * 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
  225. *
  226. * @param key 位置
  227. * @param value
  228. * 值,true为1, false为0
  229. * @return
  230. */
  231. public boolean setBit(String key, long offset, boolean value) {
  232. return redisTemplate.opsForValue().setBit(key, offset, value);
  233. }
  234. /**
  235. * 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
  236. *
  237. * @param key
  238. * @param value
  239. * @param timeout
  240. * 过期时间
  241. * @param unit
  242. * 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
  243. * 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  244. */
  245. public void setEx(String key, String value, long timeout, TimeUnit unit) {
  246. redisTemplate.opsForValue().set(key, value, timeout, unit);
  247. }
  248. /**
  249. * 只有在 key 不存在时设置 key 的值
  250. *
  251. * @param key
  252. * @param value
  253. * @return 之前已经存在返回false,不存在返回true
  254. */
  255. public boolean setIfAbsent(String key, String value) {
  256. return redisTemplate.opsForValue().setIfAbsent(key, value);
  257. }
  258. /**
  259. * 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
  260. *
  261. * @param key
  262. * @param value
  263. * @param offset
  264. * 从指定位置开始覆写
  265. */
  266. public void setRange(String key, String value, long offset) {
  267. redisTemplate.opsForValue().set(key, value, offset);
  268. }
  269. /**
  270. * 获取字符串的长度
  271. *
  272. * @param key
  273. * @return
  274. */
  275. public Long size(String key) {
  276. return redisTemplate.opsForValue().size(key);
  277. }
  278. /**
  279. * 批量添加
  280. *
  281. * @param maps
  282. */
  283. public void multiSet(Map<String, String> maps) {
  284. redisTemplate.opsForValue().multiSet(maps);
  285. }
  286. /**
  287. * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
  288. *
  289. * @param maps
  290. * @return 之前已经存在返回false,不存在返回true
  291. */
  292. public boolean multiSetIfAbsent(Map<String, String> maps) {
  293. return redisTemplate.opsForValue().multiSetIfAbsent(maps);
  294. }
  295. /**
  296. * 增加(自增长), 负数则为自减
  297. *
  298. * @param key
  299. * @return
  300. */
  301. public Long incrBy(String key, long increment) {
  302. return redisTemplate.opsForValue().increment(key, increment);
  303. }
  304. /**
  305. *
  306. * @param key
  307. * @return
  308. */
  309. public Double incrByFloat(String key, double increment) {
  310. return redisTemplate.opsForValue().increment(key, increment);
  311. }
  312. /**
  313. * 追加到末尾
  314. *
  315. * @param key
  316. * @param value
  317. * @return
  318. */
  319. public Integer append(String key, String value) {
  320. return redisTemplate.opsForValue().append(key, value);
  321. }
  322. /** -------------------hash相关操作------------------------- */
  323. /**
  324. * 获取存储在哈希表中指定字段的值
  325. *
  326. * @param key
  327. * @param field
  328. * @return
  329. */
  330. public Object hGet(String key, String field) {
  331. return redisTemplate.opsForHash().get(key, field);
  332. }
  333. /**
  334. * 获取所有给定字段的值
  335. *
  336. * @param key
  337. * @return
  338. */
  339. public Map<Object, Object> hGetAll(String key) {
  340. return redisTemplate.opsForHash().entries(key);
  341. }
  342. /**
  343. * 获取所有给定字段的值
  344. *
  345. * @param key
  346. * @param fields
  347. * @return
  348. */
  349. public List<Object> hMultiGet(String key, Collection<Object> fields) {
  350. return redisTemplate.opsForHash().multiGet(key, fields);
  351. }
  352. public void hPut(String key, String hashKey, String value) {
  353. redisTemplate.opsForHash().put(key, hashKey, value);
  354. }
  355. public void hPutAll(String key, Map<String, String> maps) {
  356. redisTemplate.opsForHash().putAll(key, maps);
  357. }
  358. /**
  359. * 仅当hashKey不存在时才设置
  360. *
  361. * @param key
  362. * @param hashKey
  363. * @param value
  364. * @return
  365. */
  366. public Boolean hPutIfAbsent(String key, String hashKey, String value) {
  367. return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
  368. }
  369. /**
  370. * 删除一个或多个哈希表字段
  371. *
  372. * @param key
  373. * @param fields
  374. * @return
  375. */
  376. public Long hDelete(String key, Object... fields) {
  377. return redisTemplate.opsForHash().delete(key, fields);
  378. }
  379. /**
  380. * 查看哈希表 key 中,指定的字段是否存在
  381. *
  382. * @param key
  383. * @param field
  384. * @return
  385. */
  386. public boolean hExists(String key, String field) {
  387. return redisTemplate.opsForHash().hasKey(key, field);
  388. }
  389. /**
  390. * 为哈希表 key 中的指定字段的整数值加上增量 increment
  391. *
  392. * @param key
  393. * @param field
  394. * @param increment
  395. * @return
  396. */
  397. public Long hIncrBy(String key, Object field, long increment) {
  398. return redisTemplate.opsForHash().increment(key, field, increment);
  399. }
  400. /**
  401. * 为哈希表 key 中的指定字段的整数值加上增量 increment
  402. *
  403. * @param key
  404. * @param field
  405. * @param delta
  406. * @return
  407. */
  408. public Double hIncrByFloat(String key, Object field, double delta) {
  409. return redisTemplate.opsForHash().increment(key, field, delta);
  410. }
  411. /**
  412. * 获取所有哈希表中的字段
  413. *
  414. * @param key
  415. * @return
  416. */
  417. public Set<Object> hKeys(String key) {
  418. return redisTemplate.opsForHash().keys(key);
  419. }
  420. /**
  421. * 获取哈希表中字段的数量
  422. *
  423. * @param key
  424. * @return
  425. */
  426. public Long hSize(String key) {
  427. return redisTemplate.opsForHash().size(key);
  428. }
  429. /**
  430. * 获取哈希表中所有值
  431. *
  432. * @param key
  433. * @return
  434. */
  435. public List<Object> hValues(String key) {
  436. return redisTemplate.opsForHash().values(key);
  437. }
  438. /**
  439. * 迭代哈希表中的键值对
  440. *
  441. * @param key
  442. * @param options
  443. * @return
  444. */
  445. public Cursor<Entry<Object, Object>> hScan(String key, ScanOptions options) {
  446. return redisTemplate.opsForHash().scan(key, options);
  447. }
  448. /** ------------------------list相关操作---------------------------- */
  449. /**
  450. * 通过索引获取列表中的元素
  451. *
  452. * @param key
  453. * @param index
  454. * @return
  455. */
  456. public String lIndex(String key, long index) {
  457. return redisTemplate.opsForList().index(key, index);
  458. }
  459. /**
  460. * 获取列表指定范围内的元素
  461. *
  462. * @param key
  463. * @param start
  464. * 开始位置, 0是开始位置
  465. * @param end
  466. * 结束位置, -1返回所有
  467. * @return
  468. */
  469. public List<String> lRange(String key, long start, long end) {
  470. return redisTemplate.opsForList().range(key, start, end);
  471. }
  472. /**
  473. * 存储在list头部
  474. *
  475. * @param key
  476. * @param value
  477. * @return
  478. */
  479. public Long lLeftPush(String key, String value) {
  480. return redisTemplate.opsForList().leftPush(key, value);
  481. }
  482. /**
  483. *
  484. * @param key
  485. * @param value
  486. * @return
  487. */
  488. public Long lLeftPushAll(String key, String... value) {
  489. return redisTemplate.opsForList().leftPushAll(key, value);
  490. }
  491. /**
  492. *
  493. * @param key
  494. * @param value
  495. * @return
  496. */
  497. public Long lLeftPushAll(String key, Collection<String> value) {
  498. return redisTemplate.opsForList().leftPushAll(key, value);
  499. }
  500. /**
  501. * 当list存在的时候才加入
  502. *
  503. * @param key
  504. * @param value
  505. * @return
  506. */
  507. public Long lLeftPushIfPresent(String key, String value) {
  508. return redisTemplate.opsForList().leftPushIfPresent(key, value);
  509. }
  510. /**
  511. * 如果pivot存在,再pivot前面添加
  512. *
  513. * @param key
  514. * @param pivot
  515. * @param value
  516. * @return
  517. */
  518. public Long lLeftPush(String key, String pivot, String value) {
  519. return redisTemplate.opsForList().leftPush(key, pivot, value);
  520. }
  521. /**
  522. *
  523. * @param key
  524. * @param value
  525. * @return
  526. */
  527. public Long lRightPush(String key, String value) {
  528. return redisTemplate.opsForList().rightPush(key, value);
  529. }
  530. /**
  531. *
  532. * @param key
  533. * @param value
  534. * @return
  535. */
  536. public Long lRightPushAll(String key, String... value) {
  537. return redisTemplate.opsForList().rightPushAll(key, value);
  538. }
  539. /**
  540. *
  541. * @param key
  542. * @param value
  543. * @return
  544. */
  545. public Long lRightPushAll(String key, Collection<String> value) {
  546. return redisTemplate.opsForList().rightPushAll(key, value);
  547. }
  548. /**
  549. * 为已存在的列表添加值
  550. *
  551. * @param key
  552. * @param value
  553. * @return
  554. */
  555. public Long lRightPushIfPresent(String key, String value) {
  556. return redisTemplate.opsForList().rightPushIfPresent(key, value);
  557. }
  558. /**
  559. * 在pivot元素的右边添加值
  560. *
  561. * @param key
  562. * @param pivot
  563. * @param value
  564. * @return
  565. */
  566. public Long lRightPush(String key, String pivot, String value) {
  567. return redisTemplate.opsForList().rightPush(key, pivot, value);
  568. }
  569. /**
  570. * 通过索引设置列表元素的值
  571. *
  572. * @param key
  573. * @param index
  574. * 位置
  575. * @param value
  576. */
  577. public void lSet(String key, long index, String value) {
  578. redisTemplate.opsForList().set(key, index, value);
  579. }
  580. /**
  581. * 移出并获取列表的第一个元素
  582. *
  583. * @param key
  584. * @return 删除的元素
  585. */
  586. public String lLeftPop(String key) {
  587. return redisTemplate.opsForList().leftPop(key);
  588. }
  589. /**
  590. * 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  591. *
  592. * @param key
  593. * @param timeout
  594. * 等待时间
  595. * @param unit
  596. * 时间单位
  597. * @return
  598. */
  599. public String lBLeftPop(String key, long timeout, TimeUnit unit) {
  600. return redisTemplate.opsForList().leftPop(key, timeout, unit);
  601. }
  602. /**
  603. * 移除并获取列表最后一个元素
  604. *
  605. * @param key
  606. * @return 删除的元素
  607. */
  608. public String lRightPop(String key) {
  609. return redisTemplate.opsForList().rightPop(key);
  610. }
  611. /**
  612. * 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  613. *
  614. * @param key
  615. * @param timeout
  616. * 等待时间
  617. * @param unit
  618. * 时间单位
  619. * @return
  620. */
  621. public String lBRightPop(String key, long timeout, TimeUnit unit) {
  622. return redisTemplate.opsForList().rightPop(key, timeout, unit);
  623. }
  624. /**
  625. * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
  626. *
  627. * @param sourceKey
  628. * @param destinationKey
  629. * @return
  630. */
  631. public String lRightPopAndLeftPush(String sourceKey, String destinationKey) {
  632. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
  633. destinationKey);
  634. }
  635. /**
  636. * 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  637. *
  638. * @param sourceKey
  639. * @param destinationKey
  640. * @param timeout
  641. * @param unit
  642. * @return
  643. */
  644. public String lBRightPopAndLeftPush(String sourceKey, String destinationKey,
  645. long timeout, TimeUnit unit) {
  646. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
  647. destinationKey, timeout, unit);
  648. }
  649. /**
  650. * 删除集合中值等于value得元素
  651. *
  652. * @param key
  653. * @param index
  654. * index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
  655. * index<0, 从尾部开始删除第一个值等于value的元素;
  656. * @param value
  657. * @return
  658. */
  659. public Long lRemove(String key, long index, String value) {
  660. return redisTemplate.opsForList().remove(key, index, value);
  661. }
  662. /**
  663. * 裁剪list
  664. *
  665. * @param key
  666. * @param start
  667. * @param end
  668. */
  669. public void lTrim(String key, long start, long end) {
  670. redisTemplate.opsForList().trim(key, start, end);
  671. }
  672. /**
  673. * 获取列表长度
  674. *
  675. * @param key
  676. * @return
  677. */
  678. public Long lLen(String key) {
  679. return redisTemplate.opsForList().size(key);
  680. }
  681. /** --------------------set相关操作-------------------------- */
  682. /**
  683. * set添加元素
  684. *
  685. * @param key
  686. * @param values
  687. * @return
  688. */
  689. public Long sAdd(String key, String... values) {
  690. return redisTemplate.opsForSet().add(key, values);
  691. }
  692. /**
  693. * set移除元素
  694. *
  695. * @param key
  696. * @param values
  697. * @return
  698. */
  699. public Long sRemove(String key, Object... values) {
  700. return redisTemplate.opsForSet().remove(key, values);
  701. }
  702. /**
  703. * 移除并返回集合的一个随机元素
  704. *
  705. * @param key
  706. * @return
  707. */
  708. public String sPop(String key) {
  709. return redisTemplate.opsForSet().pop(key);
  710. }
  711. /**
  712. * 将元素value从一个集合移到另一个集合
  713. *
  714. * @param key
  715. * @param value
  716. * @param destKey
  717. * @return
  718. */
  719. public Boolean sMove(String key, String value, String destKey) {
  720. return redisTemplate.opsForSet().move(key, value, destKey);
  721. }
  722. /**
  723. * 获取集合的大小
  724. *
  725. * @param key
  726. * @return
  727. */
  728. public Long sSize(String key) {
  729. return redisTemplate.opsForSet().size(key);
  730. }
  731. /**
  732. * 判断集合是否包含value
  733. *
  734. * @param key
  735. * @param value
  736. * @return
  737. */
  738. public Boolean sIsMember(String key, Object value) {
  739. return redisTemplate.opsForSet().isMember(key, value);
  740. }
  741. /**
  742. * 获取两个集合的交集
  743. *
  744. * @param key
  745. * @param otherKey
  746. * @return
  747. */
  748. public Set<String> sIntersect(String key, String otherKey) {
  749. return redisTemplate.opsForSet().intersect(key, otherKey);
  750. }
  751. /**
  752. * 获取key集合与多个集合的交集
  753. *
  754. * @param key
  755. * @param otherKeys
  756. * @return
  757. */
  758. public Set<String> sIntersect(String key, Collection<String> otherKeys) {
  759. return redisTemplate.opsForSet().intersect(key, otherKeys);
  760. }
  761. /**
  762. * key集合与otherKey集合的交集存储到destKey集合中
  763. *
  764. * @param key
  765. * @param otherKey
  766. * @param destKey
  767. * @return
  768. */
  769. public Long sIntersectAndStore(String key, String otherKey, String destKey) {
  770. return redisTemplate.opsForSet().intersectAndStore(key, otherKey,
  771. destKey);
  772. }
  773. /**
  774. * key集合与多个集合的交集存储到destKey集合中
  775. *
  776. * @param key
  777. * @param otherKeys
  778. * @param destKey
  779. * @return
  780. */
  781. public Long sIntersectAndStore(String key, Collection<String> otherKeys,
  782. String destKey) {
  783. return redisTemplate.opsForSet().intersectAndStore(key, otherKeys,
  784. destKey);
  785. }
  786. /**
  787. * 获取两个集合的并集
  788. *
  789. * @param key
  790. * @param otherKeys
  791. * @return
  792. */
  793. public Set<String> sUnion(String key, String otherKeys) {
  794. return redisTemplate.opsForSet().union(key, otherKeys);
  795. }
  796. /**
  797. * 获取key集合与多个集合的并集
  798. *
  799. * @param key
  800. * @param otherKeys
  801. * @return
  802. */
  803. public Set<String> sUnion(String key, Collection<String> otherKeys) {
  804. return redisTemplate.opsForSet().union(key, otherKeys);
  805. }
  806. /**
  807. * key集合与otherKey集合的并集存储到destKey中
  808. *
  809. * @param key
  810. * @param otherKey
  811. * @param destKey
  812. * @return
  813. */
  814. public Long sUnionAndStore(String key, String otherKey, String destKey) {
  815. return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
  816. }
  817. /**
  818. * key集合与多个集合的并集存储到destKey中
  819. *
  820. * @param key
  821. * @param otherKeys
  822. * @param destKey
  823. * @return
  824. */
  825. public Long sUnionAndStore(String key, Collection<String> otherKeys,
  826. String destKey) {
  827. return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
  828. }
  829. /**
  830. * 获取两个集合的差集
  831. *
  832. * @param key
  833. * @param otherKey
  834. * @return
  835. */
  836. public Set<String> sDifference(String key, String otherKey) {
  837. return redisTemplate.opsForSet().difference(key, otherKey);
  838. }
  839. /**
  840. * 获取key集合与多个集合的差集
  841. *
  842. * @param key
  843. * @param otherKeys
  844. * @return
  845. */
  846. public Set<String> sDifference(String key, Collection<String> otherKeys) {
  847. return redisTemplate.opsForSet().difference(key, otherKeys);
  848. }
  849. /**
  850. * key集合与otherKey集合的差集存储到destKey中
  851. *
  852. * @param key
  853. * @param otherKey
  854. * @param destKey
  855. * @return
  856. */
  857. public Long sDifference(String key, String otherKey, String destKey) {
  858. return redisTemplate.opsForSet().differenceAndStore(key, otherKey,
  859. destKey);
  860. }
  861. /**
  862. * key集合与多个集合的差集存储到destKey中
  863. *
  864. * @param key
  865. * @param otherKeys
  866. * @param destKey
  867. * @return
  868. */
  869. public Long sDifference(String key, Collection<String> otherKeys,
  870. String destKey) {
  871. return redisTemplate.opsForSet().differenceAndStore(key, otherKeys,
  872. destKey);
  873. }
  874. /**
  875. * 获取集合所有元素
  876. *
  877. * @param key
  878. * @return
  879. */
  880. public Set<String> setMembers(String key) {
  881. return redisTemplate.opsForSet().members(key);
  882. }
  883. /**
  884. * 随机获取集合中的一个元素
  885. *
  886. * @param key
  887. * @return
  888. */
  889. public String sRandomMember(String key) {
  890. return redisTemplate.opsForSet().randomMember(key);
  891. }
  892. /**
  893. * 随机获取集合中count个元素
  894. *
  895. * @param key
  896. * @param count
  897. * @return
  898. */
  899. public List<String> sRandomMembers(String key, long count) {
  900. return redisTemplate.opsForSet().randomMembers(key, count);
  901. }
  902. /**
  903. * 随机获取集合中count个元素并且去除重复的
  904. *
  905. * @param key
  906. * @param count
  907. * @return
  908. */
  909. public Set<String> sDistinctRandomMembers(String key, long count) {
  910. return redisTemplate.opsForSet().distinctRandomMembers(key, count);
  911. }
  912. /**
  913. *
  914. * @param key
  915. * @param options
  916. * @return
  917. */
  918. public Cursor<String> sScan(String key, ScanOptions options) {
  919. return redisTemplate.opsForSet().scan(key, options);
  920. }
  921. /**------------------zSet相关操作--------------------------------*/
  922. /**
  923. * 添加元素,有序集合是按照元素的score值由小到大排列
  924. *
  925. * @param key
  926. * @param value
  927. * @param score
  928. * @return
  929. */
  930. public Boolean zAdd(String key, String value, double score) {
  931. return redisTemplate.opsForZSet().add(key, value, score);
  932. }
  933. /**
  934. *
  935. * @param key
  936. * @param values
  937. * @return
  938. */
  939. public Long zAdd(String key, Set<TypedTuple<String>> values) {
  940. return redisTemplate.opsForZSet().add(key, values);
  941. }
  942. /**
  943. *
  944. * @param key
  945. * @param values
  946. * @return
  947. */
  948. public Long zRemove(String key, Object... values) {
  949. return redisTemplate.opsForZSet().remove(key, values);
  950. }
  951. /**
  952. * 增加元素的score值,并返回增加后的值
  953. *
  954. * @param key
  955. * @param value
  956. * @param delta
  957. * @return
  958. */
  959. public Double zIncrementScore(String key, String value, double delta) {
  960. return redisTemplate.opsForZSet().incrementScore(key, value, delta);
  961. }
  962. /**
  963. * 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
  964. *
  965. * @param key
  966. * @param value
  967. * @return 0表示第一位
  968. */
  969. public Long zRank(String key, Object value) {
  970. return redisTemplate.opsForZSet().rank(key, value);
  971. }
  972. /**
  973. * 返回元素在集合的排名,按元素的score值由大到小排列
  974. *
  975. * @param key
  976. * @param value
  977. * @return
  978. */
  979. public Long zReverseRank(String key, Object value) {
  980. return redisTemplate.opsForZSet().reverseRank(key, value);
  981. }
  982. /**
  983. * 获取集合的元素, 从小到大排序
  984. *
  985. * @param key
  986. * @param start
  987. * 开始位置
  988. * @param end
  989. * 结束位置, -1查询所有
  990. * @return
  991. */
  992. public Set<String> zRange(String key, long start, long end) {
  993. return redisTemplate.opsForZSet().range(key, start, end);
  994. }
  995. /**
  996. * 获取集合元素, 并且把score值也获取
  997. *
  998. * @param key
  999. * @param start
  1000. * @param end
  1001. * @return
  1002. */
  1003. public Set<TypedTuple<String>> zRangeWithScores(String key, long start,
  1004. long end) {
  1005. return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
  1006. }
  1007. /**
  1008. * 根据Score值查询集合元素
  1009. *
  1010. * @param key
  1011. * @param min
  1012. * 最小值
  1013. * @param max
  1014. * 最大值
  1015. * @return
  1016. */
  1017. public Set<String> zRangeByScore(String key, double min, double max) {
  1018. return redisTemplate.opsForZSet().rangeByScore(key, min, max);
  1019. }
  1020. /**
  1021. * 根据Score值查询集合元素, 从小到大排序
  1022. *
  1023. * @param key
  1024. * @param min
  1025. * 最小值
  1026. * @param max
  1027. * 最大值
  1028. * @return
  1029. */
  1030. public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
  1031. double min, double max) {
  1032. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
  1033. }
  1034. /**
  1035. *
  1036. * @param key
  1037. * @param min
  1038. * @param max
  1039. * @param start
  1040. * @param end
  1041. * @return
  1042. */
  1043. public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
  1044. double min, double max, long start, long end) {
  1045. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
  1046. start, end);
  1047. }
  1048. /**
  1049. * 获取集合的元素, 从大到小排序
  1050. *
  1051. * @param key
  1052. * @param start
  1053. * @param end
  1054. * @return
  1055. */
  1056. public Set<String> zReverseRange(String key, long start, long end) {
  1057. return redisTemplate.opsForZSet().reverseRange(key, start, end);
  1058. }
  1059. /**
  1060. * 获取集合的元素, 从大到小排序, 并返回score值
  1061. *
  1062. * @param key
  1063. * @param start
  1064. * @param end
  1065. * @return
  1066. */
  1067. public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
  1068. long start, long end) {
  1069. return redisTemplate.opsForZSet().reverseRangeWithScores(key, start,
  1070. end);
  1071. }
  1072. /**
  1073. * 根据Score值查询集合元素, 从大到小排序
  1074. *
  1075. * @param key
  1076. * @param min
  1077. * @param max
  1078. * @return
  1079. */
  1080. public Set<String> zReverseRangeByScore(String key, double min,
  1081. double max) {
  1082. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
  1083. }
  1084. /**
  1085. * 根据Score值查询集合元素, 从大到小排序
  1086. *
  1087. * @param key
  1088. * @param min
  1089. * @param max
  1090. * @return
  1091. */
  1092. public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(
  1093. String key, double min, double max) {
  1094. return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,
  1095. min, max);
  1096. }
  1097. /**
  1098. *
  1099. * @param key
  1100. * @param min
  1101. * @param max
  1102. * @param start
  1103. * @param end
  1104. * @return
  1105. */
  1106. public Set<String> zReverseRangeByScore(String key, double min,
  1107. double max, long start, long end) {
  1108. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max,
  1109. start, end);
  1110. }
  1111. /**
  1112. * 根据score值获取集合元素数量
  1113. *
  1114. * @param key
  1115. * @param min
  1116. * @param max
  1117. * @return
  1118. */
  1119. public Long zCount(String key, double min, double max) {
  1120. return redisTemplate.opsForZSet().count(key, min, max);
  1121. }
  1122. /**
  1123. * 获取集合大小
  1124. *
  1125. * @param key
  1126. * @return
  1127. */
  1128. public Long zSize(String key) {
  1129. return redisTemplate.opsForZSet().size(key);
  1130. }
  1131. /**
  1132. * 获取集合大小
  1133. *
  1134. * @param key
  1135. * @return
  1136. */
  1137. public Long zZCard(String key) {
  1138. return redisTemplate.opsForZSet().zCard(key);
  1139. }
  1140. /**
  1141. * 获取集合中value元素的score值
  1142. *
  1143. * @param key
  1144. * @param value
  1145. * @return
  1146. */
  1147. public Double zScore(String key, Object value) {
  1148. return redisTemplate.opsForZSet().score(key, value);
  1149. }
  1150. /**
  1151. * 移除指定索引位置的成员
  1152. *
  1153. * @param key
  1154. * @param start
  1155. * @param end
  1156. * @return
  1157. */
  1158. public Long zRemoveRange(String key, long start, long end) {
  1159. return redisTemplate.opsForZSet().removeRange(key, start, end);
  1160. }
  1161. /**
  1162. * 根据指定的score值的范围来移除成员
  1163. *
  1164. * @param key
  1165. * @param min
  1166. * @param max
  1167. * @return
  1168. */
  1169. public Long zRemoveRangeByScore(String key, double min, double max) {
  1170. return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
  1171. }
  1172. /**
  1173. * 获取key和otherKey的并集并存储在destKey中
  1174. *
  1175. * @param key
  1176. * @param otherKey
  1177. * @param destKey
  1178. * @return
  1179. */
  1180. public Long zUnionAndStore(String key, String otherKey, String destKey) {
  1181. return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
  1182. }
  1183. /**
  1184. *
  1185. * @param key
  1186. * @param otherKeys
  1187. * @param destKey
  1188. * @return
  1189. */
  1190. public Long zUnionAndStore(String key, Collection<String> otherKeys,
  1191. String destKey) {
  1192. return redisTemplate.opsForZSet()
  1193. .unionAndStore(key, otherKeys, destKey);
  1194. }
  1195. /**
  1196. * 交集
  1197. *
  1198. * @param key
  1199. * @param otherKey
  1200. * @param destKey
  1201. * @return
  1202. */
  1203. public Long zIntersectAndStore(String key, String otherKey,
  1204. String destKey) {
  1205. return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
  1206. destKey);
  1207. }
  1208. /**
  1209. * 交集
  1210. *
  1211. * @param key
  1212. * @param otherKeys
  1213. * @param destKey
  1214. * @return
  1215. */
  1216. public Long zIntersectAndStore(String key, Collection<String> otherKeys,
  1217. String destKey) {
  1218. return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
  1219. destKey);
  1220. }
  1221. /**
  1222. *
  1223. * @param key
  1224. * @param options
  1225. * @return
  1226. */
  1227. public Cursor<TypedTuple<String>> zScan(String key, ScanOptions options) {
  1228. return redisTemplate.opsForZSet().scan(key, options);
  1229. }
  1230. }

《参考:https://github.com/whvcse/RedisUtil》

发表评论

表情:
评论列表 (有 0 条评论,481人围观)

还没有评论,来说两句吧...

相关阅读