7.Spring Boot 中使用 Redis 数据库

2019年11月05日 09:40 · 阅读(116) ·

目标

在 Spring Boot 项目中设置,获取 Redis 值

前提条件

Redis 安装、使用

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0
Redis Redis-x64-3.2.100
RedisDesktopManager 0.8.8.384

说明

本文代码基于 6.Mybatisplus 代码生成器 的基础上进行修改

test-invoice-contract

添加接口

TRbtTestConsumer

  1. package com.test.invoice.service.consumer;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.test.invoice.model.TRbtUserEntity;
  4. import com.test.invoice.service.hystrix.NotBreakerConfiguration;
  5. import com.test.invoice.vo.LoginRequestVO;
  6. import com.test.invoice.vo.ResponseVO;
  7. import org.springframework.cloud.openfeign.FeignClient;
  8. import org.springframework.web.bind.annotation.*;
  9. import com.test.invoice.data.TRbtTestData;
  10. import com.test.invoice.data.TRbtTestDataParam;
  11. import com.test.invoice.service.hystrix.TRbtTestHystrix;
  12. /**
  13. * Feign-api 接口定义——TRbtTestConsumer
  14. *
  15. * @author:
  16. * @version:
  17. * @date: 2019-08-08 14:45
  18. */
  19. //@FeignClient(value = "service-producer")
  20. //modify by v_hwhao 20190828 加入熔断处理
  21. //@FeignClient(value = "service-producer",fallback = TRbtTestHystrix.class)
  22. //modify by v_hwhao 20190927 - 不进入熔断,直接抛出异常
  23. @FeignClient(value = "service-producer",configuration = NotBreakerConfiguration.class)
  24. public interface TRbtTestConsumer {
  25. @PostMapping("/Test/RedisTest")
  26. ResponseVO<String> RedisTest();
  27. }

TRbtTestHystrix

  1. package com.test.invoice.service.hystrix;
  2. import com.test.invoice.data.TRbtTestData;
  3. import com.test.invoice.data.TRbtTestDataParam;
  4. import com.test.invoice.enums.ResponseCode;
  5. import com.test.invoice.model.TRbtUserEntity;
  6. import com.test.invoice.service.consumer.TRbtTestConsumer;
  7. import com.test.invoice.vo.LoginRequestVO;
  8. import com.test.invoice.vo.ResponseVO;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  12. import org.springframework.web.bind.annotation.*;
  13. /**
  14. * TRbtTestHystrix - 熔断处理类
  15. *
  16. * @author:
  17. * @version:
  18. * @date: 2019-08-28 11:50
  19. */
  20. @Component
  21. public class TRbtTestHystrix implements TRbtTestConsumer {
  22. ResponseVO<String> resultString = new ResponseVO<>(ResponseCode.SYSTEM_EXCEPTION_HYSTRIX);
  23. @Override
  24. public ResponseVO<String> RedisTest(){
  25. return resultString;
  26. }
  27. }

test-invoice-service

pom.xml

  1. <!--redis start-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

application-dev.yml

  1. spring:
  2. #redis集群配置
  3. redis:
  4. # Redis服务器连接密码(默认为空)
  5. password:
  6. databsse: 1
  7. host: localhost #127.0.0.1,10.41.93.42
  8. port: 6379
  9. jedis:
  10. select: 1
  11. pool:
  12. # 连接池最大连接数(使用负值表示没有限制)
  13. max-active: 5000
  14. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  15. max-wait: -1s
  16. # 连接池中的最大空闲连接
  17. max-idle: 30
  18. # 连接池中的最小空闲连接
  19. min-idle: 5
  20. # 连接超时时间(毫秒)
  21. commandTimeout: 50000

RedisConfig

  1. package com.test.invoice.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  11. import org.springframework.data.redis.serializer.StringRedisSerializer;
  12. @Configuration
  13. public class RedisConfig{
  14. /**
  15. * 对存取的字符串进行一个json格式的系列化初始配置
  16. *
  17. * @param factory
  18. * @return
  19. */
  20. @Bean
  21. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  22. StringRedisTemplate template = new StringRedisTemplate(factory);
  23. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  24. ObjectMapper objectMapper = new ObjectMapper();
  25. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  26. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  27. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  28. // 设置key和value的序列化规则
  29. template.setKeySerializer(new StringRedisSerializer());
  30. template.setValueSerializer(jackson2JsonRedisSerializer);
  31. template.afterPropertiesSet();
  32. return template;
  33. }
  34. @Bean
  35. public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
  36. StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
  37. stringRedisTemplate.setConnectionFactory(factory);
  38. return stringRedisTemplate;
  39. }
  40. }

RedisUtil

  1. package com.test.invoice.util;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
  4. import org.springframework.stereotype.Component;
  5. import javax.annotation.Resource;
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import java.util.concurrent.TimeUnit;
  11. /**
  12. * @author
  13. * <p>
  14. * redis 操作工具类
  15. */
  16. @Component
  17. public class RedisUtil {
  18. @Resource
  19. private RedisTemplate<Object, Object> redisTemplate;
  20. public RedisTemplate<Object, Object> getInstance() {
  21. return redisTemplate;
  22. }
  23. /**
  24. * 设置 String 类型 key-value
  25. *
  26. * @param key
  27. * @param value
  28. */
  29. public void set(String key, String value) {
  30. redisTemplate.opsForValue().set(key, value);
  31. }
  32. /**
  33. * 获取 String 类型 key-value
  34. *
  35. * @param key
  36. * @return
  37. */
  38. public String get(String key) {
  39. return (String) redisTemplate.opsForValue().get(key);
  40. }
  41. /**
  42. * 设置 String 类型 key-value 并添加过期时间 (秒单位)
  43. *
  44. * @param key
  45. * @param value
  46. * @param time 过期时间,秒单位
  47. */
  48. public void setForTimeSec(String key, String value, long time) {
  49. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  50. }
  51. /**
  52. * 设置 String 类型 key-value 并添加过期时间 (毫秒单位)
  53. *
  54. * @param key
  55. * @param value
  56. * @param time 过期时间,毫秒单位
  57. */
  58. public void setForTimeMS(String key, String value, long time) {
  59. redisTemplate.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
  60. }
  61. /**
  62. * 设置 String 类型 key-value 并添加过期时间 (分钟单位)
  63. *
  64. * @param key
  65. * @param value
  66. * @param time 过期时间,分钟单位
  67. */
  68. public void setForTimeMIN(String key, String value, long time) {
  69. redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
  70. }
  71. /**
  72. * 设置 String 类型 key-value 并添加过期时间 (分钟单位)
  73. *
  74. * @param key
  75. * @param value
  76. * @param time 过期时间,分钟单位
  77. */
  78. public void setForTimeCustom(String key, String value, long time, TimeUnit type) {
  79. redisTemplate.opsForValue().set(key, value, time, type);
  80. }
  81. /**
  82. * 如果 key 存在则覆盖,并返回旧值.
  83. * 如果不存在,返回null 并添加
  84. *
  85. * @param key
  86. * @param value
  87. * @return
  88. */
  89. public String getAndSet(String key, String value) {
  90. return (String) redisTemplate.opsForValue().getAndSet(key, value);
  91. }
  92. /**
  93. * 批量添加 key-value (重复的键会覆盖)
  94. *
  95. * @param keyAndValue
  96. */
  97. public void batchSet(Map<String, String> keyAndValue) {
  98. redisTemplate.opsForValue().multiSet(keyAndValue);
  99. }
  100. /**
  101. * 批量添加 key-value 只有在键不存在时,才添加
  102. * map 中只要有一个key存在,则全部不添加
  103. *
  104. * @param keyAndValue
  105. */
  106. public void batchSetIfAbsent(Map<String, String> keyAndValue) {
  107. redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
  108. }
  109. /**
  110. * 对一个 key-value 的值进行加减操作,
  111. * 如果该 key 不存在 将创建一个key 并赋值该 number
  112. * 如果 key 存在,但 value 不是长整型 ,将报错
  113. *
  114. * @param key
  115. * @param number
  116. */
  117. public Long increment(String key, long number) {
  118. return redisTemplate.opsForValue().increment(key, number);
  119. }
  120. /**
  121. * 对一个 key-value 的值进行加减操作,
  122. * 如果该 key 不存在 将创建一个key 并赋值该 number
  123. * 如果 key 存在,但 value 不是 纯数字 ,将报错
  124. *
  125. * @param key
  126. * @param number
  127. */
  128. public Double increment(String key, double number) {
  129. return redisTemplate.opsForValue().increment(key, number);
  130. }
  131. /**
  132. * 给一个指定的 key 值附加过期时间
  133. *
  134. * @param key
  135. * @param time
  136. * @param type
  137. * @return
  138. */
  139. public boolean expire(String key, long time, TimeUnit type) {
  140. return redisTemplate.boundValueOps(key).expire(time, type);
  141. }
  142. /**
  143. * 移除指定key 的过期时间
  144. *
  145. * @param key
  146. * @return
  147. */
  148. public boolean persist(String key) {
  149. return redisTemplate.boundValueOps(key).persist();
  150. }
  151. /**
  152. * 获取指定key 的过期时间
  153. *
  154. * @param key
  155. * @return
  156. */
  157. public Long getExpire(String key) {
  158. return redisTemplate.boundValueOps(key).getExpire();
  159. }
  160. /**
  161. * 修改 key
  162. *
  163. * @param key
  164. * @return
  165. */
  166. public void rename(String key, String newKey) {
  167. redisTemplate.boundValueOps(key).rename(newKey);
  168. }
  169. /**
  170. * 删除 key-value
  171. *
  172. * @param key
  173. * @return
  174. */
  175. public boolean delete(String key) {
  176. return redisTemplate.delete(key);
  177. }
  178. //hash操作
  179. /**
  180. * 添加 Hash 键值对
  181. *
  182. * @param key
  183. * @param hashKey
  184. * @param value
  185. */
  186. public void put(String key, String hashKey, String value) {
  187. redisTemplate.opsForHash().put(key, hashKey, value);
  188. }
  189. /**
  190. * 批量添加 hash 的 键值对
  191. * 有则覆盖,没有则添加
  192. *
  193. * @param key
  194. * @param map
  195. */
  196. public void putAll(String key, Map<String, String> map) {
  197. redisTemplate.opsForHash().putAll(key, map);
  198. }
  199. /**
  200. * 添加 hash 键值对. 不存在的时候才添加
  201. *
  202. * @param key
  203. * @param hashKey
  204. * @param value
  205. * @return
  206. */
  207. public boolean putIfAbsent(String key, String hashKey, String value) {
  208. return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
  209. }
  210. /**
  211. * 删除指定 hash 的 HashKey
  212. *
  213. * @param key
  214. * @param hashKeys
  215. * @return 删除成功的 数量
  216. */
  217. public Long delete(String key, String... hashKeys) {
  218. return redisTemplate.opsForHash().delete(key, hashKeys);
  219. }
  220. /**
  221. * 给指定 hash 的 hashkey 做增减操作
  222. *
  223. * @param key
  224. * @param hashKey
  225. * @param number
  226. * @return
  227. */
  228. public Long increment(String key, String hashKey, long number) {
  229. return redisTemplate.opsForHash().increment(key, hashKey, number);
  230. }
  231. /**
  232. * 给指定 hash 的 hashkey 做增减操作
  233. *
  234. * @param key
  235. * @param hashKey
  236. * @param number
  237. * @return
  238. */
  239. public Double increment(String key, String hashKey, Double number) {
  240. return redisTemplate.opsForHash().increment(key, hashKey, number);
  241. }
  242. /**
  243. * 获取指定 key 下的 hashkey
  244. *
  245. * @param key
  246. * @param hashKey
  247. * @return
  248. */
  249. public Object getHashKey(String key, String hashKey) {
  250. return redisTemplate.opsForHash().get(key, hashKey);
  251. }
  252. /**
  253. * 获取 key 下的 所有 hashkey 和 value
  254. *
  255. * @param key
  256. * @return
  257. */
  258. public Map<Object, Object> getHashEntries(String key) {
  259. return redisTemplate.opsForHash().entries(key);
  260. }
  261. /**
  262. * 验证指定 key 下 有没有指定的 hashkey
  263. *
  264. * @param key
  265. * @param hashKey
  266. * @return
  267. */
  268. public boolean hashKey(String key, String hashKey) {
  269. return redisTemplate.opsForHash().hasKey(key, hashKey);
  270. }
  271. /**
  272. * 获取 key 下的 所有 hashkey 字段名
  273. *
  274. * @param key
  275. * @return
  276. */
  277. public Set<Object> hashKeys(String key) {
  278. return redisTemplate.opsForHash().keys(key);
  279. }
  280. /**
  281. * 获取指定 hash 下面的 键值对 数量
  282. *
  283. * @param key
  284. * @return
  285. */
  286. public Long hashSize(String key) {
  287. return redisTemplate.opsForHash().size(key);
  288. }
  289. //List 操作
  290. /**
  291. * 指定 list 从左入栈
  292. *
  293. * @param key
  294. * @return 当前队列的长度
  295. */
  296. public Long leftPush(String key, Object value) {
  297. return redisTemplate.opsForList().leftPush(key, value);
  298. }
  299. /**
  300. * 指定 list 从左出栈
  301. * 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
  302. *
  303. * @param key
  304. * @return 出栈的值
  305. */
  306. public Object leftPop(String key) {
  307. return redisTemplate.opsForList().leftPop(key);
  308. }
  309. /**
  310. * 从左边依次入栈
  311. * 导入顺序按照 Collection 顺序
  312. * 如: a b c => c b a
  313. *
  314. * @param key
  315. * @param values
  316. * @return
  317. */
  318. public Long leftPushAll(String key, Collection<Object> values) {
  319. return redisTemplate.opsForList().leftPushAll(key, values);
  320. }
  321. /**
  322. * 指定 list 从右入栈
  323. *
  324. * @param key
  325. * @return 当前队列的长度
  326. */
  327. public Long rightPush(String key, Object value) {
  328. return redisTemplate.opsForList().rightPush(key, value);
  329. }
  330. /**
  331. * 指定 list 从右出栈
  332. * 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
  333. *
  334. * @param key
  335. * @return 出栈的值
  336. */
  337. public Object rightPop(String key) {
  338. return redisTemplate.opsForList().rightPop(key);
  339. }
  340. /**
  341. * 从右边依次入栈
  342. * 导入顺序按照 Collection 顺序
  343. * 如: a b c => a b c
  344. *
  345. * @param key
  346. * @param values
  347. * @return
  348. */
  349. public Long rightPushAll(String key, Collection<Object> values) {
  350. return redisTemplate.opsForList().rightPushAll(key, values);
  351. }
  352. /**
  353. * 根据下标获取值
  354. *
  355. * @param key
  356. * @param index
  357. * @return
  358. */
  359. public Object popIndex(String key, long index) {
  360. return redisTemplate.opsForList().index(key, index);
  361. }
  362. /**
  363. * 获取列表指定长度
  364. *
  365. * @param key
  366. * @param index
  367. * @return
  368. */
  369. public Long listSize(String key, long index) {
  370. return redisTemplate.opsForList().size(key);
  371. }
  372. /**
  373. * 获取列表 指定范围内的所有值
  374. *
  375. * @param key
  376. * @param start
  377. * @param end
  378. * @return
  379. */
  380. public List<Object> listRange(String key, long start, long end) {
  381. return redisTemplate.opsForList().range(key, start, end);
  382. }
  383. /**
  384. * 删除 key 中 值为 value 的 count 个数.
  385. *
  386. * @param key
  387. * @param count
  388. * @param value
  389. * @return 成功删除的个数
  390. */
  391. public Long listRemove(String key, long count, Object value) {
  392. return redisTemplate.opsForList().remove(key, count, value);
  393. }
  394. /**
  395. * 删除 列表 [start,end] 以外的所有元素
  396. *
  397. * @param key
  398. * @param start
  399. * @param end
  400. */
  401. public void listTrim(String key, long start, long end) {
  402. redisTemplate.opsForList().trim(key, start, end);
  403. }
  404. /**
  405. * 将 key 右出栈,并左入栈到 key2
  406. *
  407. * @param key 右出栈的列表
  408. * @param key2 左入栈的列表
  409. * @return 操作的值
  410. */
  411. public Object rightPopAndLeftPush(String key, String key2) {
  412. return redisTemplate.opsForList().rightPopAndLeftPush(key, key2);
  413. }
  414. //set 操作 无序不重复集合
  415. /**
  416. * 添加 set 元素
  417. *
  418. * @param key
  419. * @param values
  420. * @return
  421. */
  422. public Long add(String key, String... values) {
  423. return redisTemplate.opsForSet().add(key, values);
  424. }
  425. /**
  426. * 获取两个集合的差集
  427. *
  428. * @param key
  429. * @param otherkey
  430. * @return
  431. */
  432. public Set<Object> difference(String key, String otherkey) {
  433. return redisTemplate.opsForSet().difference(key, otherkey);
  434. }
  435. /**
  436. * 获取 key 和 集合 collections 中的 key 集合的差集
  437. *
  438. * @param key
  439. * @param otherKeys
  440. * @return
  441. */
  442. public Set<Object> difference(String key, Collection<Object> otherKeys) {
  443. return redisTemplate.opsForSet().difference(key, otherKeys);
  444. }
  445. /**
  446. * 将 key 与 otherkey 的差集 ,添加到新的 newKey 集合中
  447. *
  448. * @param key
  449. * @param otherkey
  450. * @param newKey
  451. * @return 返回差集的数量
  452. */
  453. public Long differenceAndStore(String key, String otherkey, String newKey) {
  454. return redisTemplate.opsForSet().differenceAndStore(key, otherkey, newKey);
  455. }
  456. /**
  457. * 将 key 和 集合 collections 中的 key 集合的差集 添加到 newkey 集合中
  458. *
  459. * @param key
  460. * @param otherKeys
  461. * @param newKey
  462. * @return 返回差集的数量
  463. */
  464. public Long differenceAndStore(String key, Collection<Object> otherKeys, String newKey) {
  465. return redisTemplate.opsForSet().differenceAndStore(newKey, otherKeys, newKey);
  466. }
  467. /**
  468. * 删除一个或多个集合中的指定值
  469. *
  470. * @param key
  471. * @param values
  472. * @return 成功删除数量
  473. */
  474. public Long remove(String key, Object... values) {
  475. return redisTemplate.opsForSet().remove(key, values);
  476. }
  477. /**
  478. * 随机移除一个元素,并返回出来
  479. *
  480. * @param key
  481. * @return
  482. */
  483. public Object randomSetPop(String key) {
  484. return redisTemplate.opsForSet().pop(key);
  485. }
  486. /**
  487. * 随机获取一个元素
  488. *
  489. * @param key
  490. * @return
  491. */
  492. public Object randomSet(String key) {
  493. return redisTemplate.opsForSet().randomMember(key);
  494. }
  495. /**
  496. * 随机获取指定数量的元素,同一个元素可能会选中两次
  497. *
  498. * @param key
  499. * @param count
  500. * @return
  501. */
  502. public List<Object> randomSet(String key, long count) {
  503. return redisTemplate.opsForSet().randomMembers(key, count);
  504. }
  505. /**
  506. * 随机获取指定数量的元素,去重(同一个元素只能选择两一次)
  507. *
  508. * @param key
  509. * @param count
  510. * @return
  511. */
  512. public Set<Object> randomSetDistinct(String key, long count) {
  513. return redisTemplate.opsForSet().distinctRandomMembers(key, count);
  514. }
  515. /**
  516. * 将 key 中的 value 转入到 destKey 中
  517. *
  518. * @param key
  519. * @param value
  520. * @param destKey
  521. * @return 返回成功与否
  522. */
  523. public boolean moveSet(String key, Object value, String destKey) {
  524. return redisTemplate.opsForSet().move(key, value, destKey);
  525. }
  526. /**
  527. * 无序集合的大小
  528. *
  529. * @param key
  530. * @return
  531. */
  532. public Long setSize(String key) {
  533. return redisTemplate.opsForSet().size(key);
  534. }
  535. /**
  536. * 判断 set 集合中 是否有 value
  537. *
  538. * @param key
  539. * @param value
  540. * @return
  541. */
  542. public boolean isMember(String key, Object value) {
  543. return redisTemplate.opsForSet().isMember(key, value);
  544. }
  545. /**
  546. * 返回 key 和 othere 的并集
  547. *
  548. * @param key
  549. * @param otherKey
  550. * @return
  551. */
  552. public Set<Object> unionSet(String key, String otherKey) {
  553. return redisTemplate.opsForSet().union(key, otherKey);
  554. }
  555. /**
  556. * 返回 key 和 otherKeys 的并集
  557. *
  558. * @param key
  559. * @param otherKeys key 的集合
  560. * @return
  561. */
  562. public Set<Object> unionSet(String key, Collection<Object> otherKeys) {
  563. return redisTemplate.opsForSet().union(key, otherKeys);
  564. }
  565. /**
  566. * 将 key 与 otherKey 的并集,保存到 destKey 中
  567. *
  568. * @param key
  569. * @param otherKey
  570. * @param destKey
  571. * @return destKey 数量
  572. */
  573. public Long unionAndStoreSet(String key, String otherKey, String destKey) {
  574. return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
  575. }
  576. /**
  577. * 将 key 与 otherKey 的并集,保存到 destKey 中
  578. *
  579. * @param key
  580. * @param otherKeys
  581. * @param destKey
  582. * @return destKey 数量
  583. */
  584. public Long unionAndStoreSet(String key, Collection<Object> otherKeys, String destKey) {
  585. return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
  586. }
  587. /**
  588. * 返回集合中所有元素
  589. *
  590. * @param key
  591. * @return
  592. */
  593. public Set<Object> members(String key) {
  594. return redisTemplate.opsForSet().members(key);
  595. }
  596. //Zset 根据 socre 排序 不重复 每个元素附加一个 socre double类型的属性(double 可以重复)
  597. /**
  598. * 添加 ZSet 元素
  599. *
  600. * @param key
  601. * @param value
  602. * @param score
  603. */
  604. public boolean add(String key, Object value, double score) {
  605. return redisTemplate.opsForZSet().add(key, value, score);
  606. }
  607. /**
  608. * 批量添加 Zset <br>
  609. * Set<TypedTuple<Object>> tuples = new HashSet<>();<br>
  610. * TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);<br>
  611. * tuples.add(objectTypedTuple1);
  612. *
  613. * @param key
  614. * @param tuples
  615. * @return
  616. */
  617. public Long batchAddZset(String key, Set<TypedTuple<Object>> tuples) {
  618. return redisTemplate.opsForZSet().add(key, tuples);
  619. }
  620. /**
  621. * Zset 删除一个或多个元素
  622. *
  623. * @param key
  624. * @param values
  625. * @return
  626. */
  627. public Long removeZset(String key, String... values) {
  628. return redisTemplate.opsForZSet().remove(key, values);
  629. }
  630. /**
  631. * 对指定的 zset 的 value 值 , socre 属性做增减操作
  632. *
  633. * @param key
  634. * @param value
  635. * @param score
  636. * @return
  637. */
  638. public Double incrementScore(String key, Object value, double score) {
  639. return redisTemplate.opsForZSet().incrementScore(key, value, score);
  640. }
  641. /**
  642. * 获取 key 中指定 value 的排名(从0开始,从小到大排序)
  643. *
  644. * @param key
  645. * @param value
  646. * @return
  647. */
  648. public Long rank(String key, Object value) {
  649. return redisTemplate.opsForZSet().rank(key, value);
  650. }
  651. /**
  652. * 获取 key 中指定 value 的排名(从0开始,从大到小排序)
  653. *
  654. * @param key
  655. * @param value
  656. * @return
  657. */
  658. public Long reverseRank(String key, Object value) {
  659. return redisTemplate.opsForZSet().reverseRank(key, value);
  660. }
  661. /**
  662. * 获取索引区间内的排序结果集合(从0开始,从小到大,带上分数)
  663. *
  664. * @param key
  665. * @param start
  666. * @param end
  667. * @return
  668. */
  669. public Set<TypedTuple<Object>> rangeWithScores(String key, long start, long end) {
  670. return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
  671. }
  672. /**
  673. * 获取索引区间内的排序结果集合(从0开始,从小到大,只有列名)
  674. *
  675. * @param key
  676. * @param start
  677. * @param end
  678. * @return
  679. */
  680. public Set<Object> range(String key, long start, long end) {
  681. return redisTemplate.opsForZSet().range(key, start, end);
  682. }
  683. /**
  684. * 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,只有列名)
  685. *
  686. * @param key
  687. * @param min
  688. * @param max
  689. * @return
  690. */
  691. public Set<Object> rangeByScore(String key, double min, double max) {
  692. return redisTemplate.opsForZSet().rangeByScore(key, min, max);
  693. }
  694. /**
  695. * 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,集合带分数)
  696. *
  697. * @param key
  698. * @param min
  699. * @param max
  700. * @return
  701. */
  702. public Set<TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max) {
  703. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
  704. }
  705. /**
  706. * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,不带分数的集合)
  707. *
  708. * @param key
  709. * @param min
  710. * @param max
  711. * @param offset 从指定下标开始
  712. * @param count 输出指定元素数量
  713. * @return
  714. */
  715. public Set<Object> rangeByScore(String key, double min, double max, long offset, long count) {
  716. return redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
  717. }
  718. /**
  719. * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,带分数的集合)
  720. *
  721. * @param key
  722. * @param min
  723. * @param max
  724. * @param offset 从指定下标开始
  725. * @param count 输出指定元素数量
  726. * @return
  727. */
  728. public Set<TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max, long offset, long count) {
  729. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max, offset, count);
  730. }
  731. /**
  732. * 获取索引区间内的排序结果集合(从0开始,从大到小,只有列名)
  733. *
  734. * @param key
  735. * @param start
  736. * @param end
  737. * @return
  738. */
  739. public Set<Object> reverseRange(String key, long start, long end) {
  740. return redisTemplate.opsForZSet().reverseRange(key, start, end);
  741. }
  742. /**
  743. * 获取索引区间内的排序结果集合(从0开始,从大到小,带上分数)
  744. *
  745. * @param key
  746. * @param start
  747. * @param end
  748. * @return
  749. */
  750. public Set<TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) {
  751. return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
  752. }
  753. /**
  754. * 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合不带分数)
  755. *
  756. * @param key
  757. * @param min
  758. * @param max
  759. * @return
  760. */
  761. public Set<Object> reverseRangeByScore(String key, double min, double max) {
  762. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
  763. }
  764. /**
  765. * 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合带分数)
  766. *
  767. * @param key
  768. * @param min
  769. * @param max
  770. * @return
  771. */
  772. public Set<TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) {
  773. return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
  774. }
  775. /**
  776. * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,不带分数的集合)
  777. *
  778. * @param key
  779. * @param min
  780. * @param max
  781. * @param offset 从指定下标开始
  782. * @param count 输出指定元素数量
  783. * @return
  784. */
  785. public Set<Object> reverseRangeByScore(String key, double min, double max, long offset, long count) {
  786. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
  787. }
  788. /**
  789. * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,带分数的集合)
  790. *
  791. * @param key
  792. * @param min
  793. * @param max
  794. * @param offset 从指定下标开始
  795. * @param count 输出指定元素数量
  796. * @return
  797. */
  798. public Set<TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) {
  799. return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
  800. }
  801. /**
  802. * 返回指定分数区间 [min,max] 的元素个数
  803. *
  804. * @param key
  805. * @param min
  806. * @param max
  807. * @return
  808. */
  809. public long countZSet(String key, double min, double max) {
  810. return redisTemplate.opsForZSet().count(key, min, max);
  811. }
  812. /**
  813. * 返回 zset 集合数量
  814. *
  815. * @param key
  816. * @return
  817. */
  818. public long sizeZset(String key) {
  819. return redisTemplate.opsForZSet().size(key);
  820. }
  821. /**
  822. * 获取指定成员的 score 值
  823. *
  824. * @param key
  825. * @param value
  826. * @return
  827. */
  828. public Double score(String key, Object value) {
  829. return redisTemplate.opsForZSet().score(key, value);
  830. }
  831. /**
  832. * 删除指定索引位置的成员,其中成员分数按( 从小到大 )
  833. *
  834. * @param key
  835. * @param start
  836. * @param end
  837. * @return
  838. */
  839. public Long removeRange(String key, long start, long end) {
  840. return redisTemplate.opsForZSet().removeRange(key, start, end);
  841. }
  842. /**
  843. * 删除指定 分数范围 内的成员 [main,max],其中成员分数按( 从小到大 )
  844. *
  845. * @param key
  846. * @param min
  847. * @param max
  848. * @return
  849. */
  850. public Long removeRangeByScore(String key, double min, double max) {
  851. return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
  852. }
  853. /**
  854. * key 和 other 两个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
  855. *
  856. * @param key
  857. * @param otherKey
  858. * @param destKey
  859. * @return
  860. */
  861. public Long unionAndStoreZset(String key, String otherKey, String destKey) {
  862. return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
  863. }
  864. /**
  865. * key 和 otherKeys 多个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
  866. *
  867. * @param key
  868. * @param otherKeys
  869. * @param destKey
  870. * @return
  871. */
  872. public Long unionAndStoreZset(String key, Collection<String> otherKeys, String destKey) {
  873. return redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);
  874. }
  875. /**
  876. * key 和 otherKey 两个集合的交集,保存在 destKey 集合中
  877. *
  878. * @param key
  879. * @param otherKey
  880. * @param destKey
  881. * @return
  882. */
  883. public Long intersectAndStore(String key, String otherKey, String destKey) {
  884. return redisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey);
  885. }
  886. /**
  887. * key 和 otherKeys 多个集合的交集,保存在 destKey 集合中
  888. *
  889. * @param key
  890. * @param otherKeys
  891. * @param destKey
  892. * @return
  893. */
  894. public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) {
  895. return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey);
  896. }
  897. }

TRbtTestController

  1. package com.test.invoice.contorller;
  2. import com.sun.xml.internal.fastinfoset.algorithm.BooleanEncodingAlgorithm;
  3. import com.test.invoice.data.TRbtTestData;
  4. import com.test.invoice.data.TRbtTestDataParam;
  5. import com.test.invoice.enums.ResponseCode;
  6. import com.test.invoice.model.TRbtUserEntity;
  7. import com.test.invoice.service.producer.ITRbtTestService;
  8. import com.test.invoice.util.RedisUtil;
  9. import com.test.invoice.vo.LoginRequestVO;
  10. import com.test.invoice.vo.ResponseVO;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.springframework.web.bind.annotation.*;
  19. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  20. import javax.annotation.Resource;
  21. /**
  22. *
  23. * 生产者:业务微服务-测试框架系统控制类
  24. *
  25. * @author:
  26. * @version:
  27. * @date: 2019-08-12 09:53
  28. */
  29. @RestController
  30. @Api(description = "测试框架系统控制类")
  31. public class TRbtTestController {
  32. @Autowired
  33. private RedisUtil redisUtil;
  34. @PostMapping("/Test/RedisTest")
  35. @ApiOperation(value="系统框架测试-RedisTest",httpMethod = "POST",response = ResponseVO.class,notes = "系统框架测试-RedisTest")
  36. public ResponseVO<String> RedisTest(){
  37. ResponseVO<String> responseVO = new ResponseVO<>();
  38. //设置 Redis
  39. redisUtil.set("luomaKey","luomaValue");
  40. //获取 Redis
  41. String value = redisUtil.get("luomaKey");
  42. responseVO.setCode(ResponseCode.OK.value());
  43. responseVO.setMsg(ResponseCode.OK.getDescription());
  44. responseVO.setData(value);
  45. return responseVO;
  46. }
  47. }

使用 Postman 测试

直接访问

http://localhost:18800/Test/RedisTest

结果

  1. {
  2. "code": 2000,
  3. "msg": "Success",
  4. "data": "luomaValue"
  5. }

使用 RedisDesktopManager 查看 Redis 值