1.前言

  习惯使用springMVC 配置 redis ,现在使用spring boot ,得好好总结怎么在spring boot 配置和使用 ,区别真的挺大的。

2.环境

spring boot  2.1.6.RELEASE

Redis 3.2.100 -win64

jdk 1.8.0_221

3.操作

(1)目录结构

(2)pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>provider-redis-8003</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>provider-redis-8003</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <!--redis依赖-->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-data-redis</artifactId>
  26. </dependency>
  27.  
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. <exclusions>
  38. <exclusion>
  39. <groupId>org.junit.vintage</groupId>
  40. <artifactId>junit-vintage-engine</artifactId>
  41. </exclusion>
  42. </exclusions>
  43. </dependency>
  44.  
  45. <dependency>
  46. <groupId>org.apache.commons</groupId>
  47. <artifactId>commons-lang3</artifactId>
  48. <version>3.9</version>
  49. </dependency>
  50. </dependencies>
  51.  
  52. <build>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-maven-plugin</artifactId>
  57. </plugin>
  58. </plugins>
  59. </build>
  60.  
  61. </project>

(3) application.properties

  1. spring.application.name=provider-redis-8003
  2. server.port=8003
  3.  
  4. #redis配置
  5. #Redis服务器地址
  6. spring.redis.host=127.0.0.1
  7. #Redis服务器连接端口
  8. spring.redis.port=6379
  9. #Redis数据库索引(默认为0)
  10. spring.redis.database=0
  11. # Redis服务器连接密码(默认为空)
  12. spring.redis.password=666666
  13. #连接池最大连接数(使用负值表示没有限制)
  14. spring.redis.jedis.pool.max-active=600
  15. #连接池最大阻塞等待时间(使用负值表示没有限制)
  16. spring.redis.jedis.pool.max-wait=3000
  17. #连接池中的最大空闲连接
  18. spring.redis.jedis.pool.max-idle=300
  19. #连接池中的最小空闲连接
  20. spring.redis.jedis.pool.min-idle=2
  21. #连接超时时间(毫秒)
  22. spring.redis.timeout=10000

(4)redis配置类

  1. package com.example.providerredis8003.config;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  4. import com.fasterxml.jackson.annotation.PropertyAccessor;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.springframework.cache.annotation.EnableCaching;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.redis.connection.RedisConnectionFactory;
  10. import org.springframework.data.redis.core.*;
  11. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  12. import org.springframework.data.redis.serializer.StringRedisSerializer;
  13.  
  14. /**
  15. * redis配置类
  16. */
  17. //标记为配置类
  18. @Configuration
  19. //开启缓存
  20. @EnableCaching
  21. public class RedisConfig {
  22.  
  23. @Bean
  24. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  25.  
  26. /**
  27. * RedisTemplate 相关配置
  28. */
  29. RedisTemplate<String, Object> template = new RedisTemplate<>();
  30. // 配置连接工厂
  31. template.setConnectionFactory(factory);
  32.  
  33. //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  34. Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
  35.  
  36. ObjectMapper om = new ObjectMapper();
  37. // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
  38. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  39. // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
  40. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  41. jacksonSeial.setObjectMapper(om);
  42.  
  43. // 值采用json序列化
  44. template.setValueSerializer(jacksonSeial);
  45. //使用StringRedisSerializer来序列化和反序列化redis的key值
  46. template.setKeySerializer(new StringRedisSerializer());
  47.  
  48. // 设置hash key 和value序列化模式
  49. template.setHashKeySerializer(new StringRedisSerializer());
  50. template.setHashValueSerializer(jacksonSeial);
  51. template.afterPropertiesSet();
  52.  
  53. return template;
  54. }
  55.  
  56. /**
  57. * 对hash类型的数据操作
  58. *
  59. * @param redisTemplate
  60. * @return
  61. */
  62. @Bean
  63. public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
  64. return redisTemplate.opsForHash();
  65. }
  66. /**
  67. * 对redis字符串类型数据操作
  68. *
  69. * @param redisTemplate
  70. * @return
  71. */
  72. @Bean
  73. public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
  74. return redisTemplate.opsForValue();
  75. }
  76.  
  77. /**
  78. * 对链表类型的数据操作
  79. *
  80. * @param redisTemplate
  81. * @return
  82. */
  83. @Bean
  84. public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
  85. return redisTemplate.opsForList();
  86. }
  87.  
  88. /**
  89. * 对无序集合类型的数据操作
  90. *
  91. * @param redisTemplate
  92. * @return
  93. */
  94. @Bean
  95. public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
  96. return redisTemplate.opsForSet();
  97. }
  98.  
  99. /**
  100. * 对有序集合类型的数据操作
  101. *
  102. * @param redisTemplate
  103. * @return
  104. */
  105. @Bean
  106. public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
  107. return redisTemplate.opsForZSet();
  108. }
  109. }

(5)封装好的部分redis工具 ,还需要什么可以自己加

  1. package com.example.providerredis8003.util;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.data.redis.core.ZSetOperations;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.util.CollectionUtils;
  8.  
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. /**
  15. * redisTemplate封装
  16. *
  17. * @author zjjlive@dist.com.cn
  18. */
  19. @Component
  20. public class RedisUtil {
  21. /*
  22. string 95行
  23. hash 168行
  24. list 397
  25. set 303
  26. sorct set
  27.  
  28. */
  29.  
  30. @Autowired
  31. private RedisTemplate<String, Object> redisTemplate;
  32.  
  33. public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
  34. this.redisTemplate = redisTemplate;
  35. }
  36.  
  37. /**
  38. * 指定缓存失效时间
  39. *
  40. * @param key 键
  41. * @param time 时间(秒)
  42. * @return
  43. */
  44. public boolean expire(String key, long time) {
  45. try {
  46. if (time > 0) {
  47. redisTemplate.expire(key, time, TimeUnit.SECONDS);
  48. }
  49. return true;
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. return false;
  53. }
  54. }
  55.  
  56. /**
  57. * 根据key 获取过期时间
  58. *
  59. * @param key 键 不能为null
  60. * @return 时间(秒) 返回0代表为永久有效
  61. */
  62. public long getExpire(String key) {
  63. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  64. }
  65.  
  66. /**
  67. * 判断key是否存在
  68. *
  69. * @param key 键
  70. * @return true 存在 false不存在
  71. */
  72. public boolean hasKey(String key) {
  73. try {
  74. return redisTemplate.hasKey(key);
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. return false;
  78. }
  79. }
  80.  
  81. /**
  82. * 删除缓存
  83. *
  84. * @param key 可以传一个值 或多个
  85. */
  86. @SuppressWarnings("unchecked")
  87. public void del(String... key) {
  88. if (key != null && key.length > 0) {
  89. if (key.length == 1) {
  90. redisTemplate.delete(key[0]);
  91. } else {
  92. redisTemplate.delete(CollectionUtils.arrayToList(key));
  93. }
  94. }
  95. }
  96.  
  97. /**
  98. * 查找所有匹配给定的模式的键
  99. */
  100. public Set<String> keys(String pattern){
  101. return redisTemplate.keys(pattern);
  102. }
  103.  
  104. //============================String=============================
  105.  
  106. /**
  107. * 普通缓存获取
  108. *
  109. * @param key 键
  110. * @return 值
  111. */
  112. public Object get(String key) {
  113. return key == null ? null : redisTemplate.opsForValue().get(key);
  114. }
  115.  
  116. /**
  117. * 普通缓存放入
  118. *
  119. * @param key 键
  120. * @param value 值
  121. * @return true成功 false失败
  122. */
  123. public boolean set(String key, Object value) {
  124. try {
  125. redisTemplate.opsForValue().set(key, value);
  126. return true;
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. return false;
  130. }
  131. }
  132.  
  133. /**
  134. * 普通缓存放入并设置时间
  135. *
  136. * @param key 键
  137. * @param value 值
  138. * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
  139. * @return true成功 false 失败
  140. */
  141. public boolean set(String key, Object value, long time) {
  142. try {
  143. if (time > 0) {
  144. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  145. } else {
  146. set(key, value);
  147. }
  148. return true;
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. return false;
  152. }
  153. }
  154.  
  155. /**
  156. * 递增
  157. *
  158. * @param key 键
  159. * @param delta 要增加几(大于0)
  160. * @return
  161. */
  162. public long incr(String key, long delta) {
  163. if (delta < 0) {
  164. throw new RuntimeException("递增因子必须大于0");
  165. }
  166. return redisTemplate.opsForValue().increment(key, delta);
  167. }
  168.  
  169. /**
  170. * 递减
  171. *
  172. * @param key 键
  173. * @param delta 要减少几(小于0)
  174. * @return
  175. */
  176. public long decr(String key, long delta) {
  177. if (delta < 0) {
  178. throw new RuntimeException("递减因子必须大于0");
  179. }
  180. return redisTemplate.opsForValue().increment(key, -delta);
  181. }
  182.  
  183. //================================Map=================================
  184.  
  185. /**
  186. * HashGet
  187. *
  188. * @param key 键 不能为null
  189. * @param item 项 不能为null
  190. * @return 值
  191. */
  192. public Object hget(String key, String item) {
  193. return redisTemplate.opsForHash().get(key, item);
  194. }
  195.  
  196. /**
  197. * 获取hashKey对应的所有键值
  198. *
  199. * @param key 键
  200. * @return 对应的多个键值
  201. */
  202. public Map<Object, Object> hmget(String key) {
  203. return redisTemplate.opsForHash().entries(key);
  204. }
  205.  
  206. /**
  207. * HashSet
  208. *
  209. * @param key 键
  210. * @param map 对应多个键值
  211. * @return true 成功 false 失败
  212. */
  213. public boolean hmset(String key, Map<String, Object> map) {
  214. try {
  215. redisTemplate.opsForHash().putAll(key, map);
  216. return true;
  217. } catch (Exception e) {
  218. e.printStackTrace();
  219. return false;
  220. }
  221. }
  222.  
  223. /**
  224. * HashSet 并设置时间
  225. *
  226. * @param key 键
  227. * @param map 对应多个键值
  228. * @param time 时间(秒)
  229. * @return true成功 false失败
  230. */
  231. public boolean hmset(String key, Map<String, Object> map, long time) {
  232. try {
  233. redisTemplate.opsForHash().putAll(key, map);
  234. if (time > 0) {
  235. expire(key, time);
  236. }
  237. return true;
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. return false;
  241. }
  242. }
  243.  
  244. /**
  245. * 向一张hash表中放入数据,如果不存在将创建
  246. *
  247. * @param key 键
  248. * @param item 项
  249. * @param value 值
  250. * @return true 成功 false失败
  251. */
  252. public boolean hset(String key, String item, Object value) {
  253. try {
  254. redisTemplate.opsForHash().put(key, item, value);
  255. return true;
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. return false;
  259. }
  260. }
  261.  
  262. /**
  263. * 向一张hash表中放入数据,如果不存在将创建
  264. *
  265. * @param key 键
  266. * @param item 项
  267. * @param value 值
  268. * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
  269. * @return true 成功 false失败
  270. */
  271. public boolean hset(String key, String item, Object value, long time) {
  272. try {
  273. redisTemplate.opsForHash().put(key, item, value);
  274. if (time > 0) {
  275. expire(key, time);
  276. }
  277. return true;
  278. } catch (Exception e) {
  279. e.printStackTrace();
  280. return false;
  281. }
  282. }
  283.  
  284. /**
  285. * 删除hash表中的值
  286. *
  287. * @param key 键 不能为null
  288. * @param item 项 可以使多个 不能为null
  289. */
  290. public void hdel(String key, Object... item) {
  291. redisTemplate.opsForHash().delete(key, item);
  292. }
  293.  
  294. /**
  295. * 判断hash表中是否有该项的值
  296. *
  297. * @param key 键 不能为null
  298. * @param item 项 不能为null
  299. * @return true 存在 false不存在
  300. */
  301. public boolean hHasKey(String key, String item) {
  302. return redisTemplate.opsForHash().hasKey(key, item);
  303. }
  304.  
  305. /**
  306. * hash递增 如果不存在,就会创建一个 并把新增后的值返回
  307. *
  308. * @param key 键
  309. * @param item 项
  310. * @param by 要增加几(大于0)
  311. * @return
  312. */
  313. public double hincr(String key, String item, double by) {
  314. return redisTemplate.opsForHash().increment(key, item, by);
  315. }
  316.  
  317. /**
  318. * hash递减
  319. *
  320. * @param key 键
  321. * @param item 项
  322. * @param by 要减少记(小于0)
  323. * @return
  324. */
  325. public double hdecr(String key, String item, double by) {
  326. return redisTemplate.opsForHash().increment(key, item, -by);
  327. }
  328.  
  329. //===============================list=================================
  330.  
  331. /**
  332. * 获取list缓存的内容
  333. *
  334. * @param key 键
  335. * @param start 开始
  336. * @param end 结束 0 到 -1代表所有值
  337. * @return
  338. */
  339. public List<Object> lGet(String key, long start, long end) {
  340. try {
  341. return redisTemplate.opsForList().range(key, start, end);
  342. } catch (Exception e) {
  343. e.printStackTrace();
  344. return null;
  345. }
  346. }
  347.  
  348. /**
  349. * 获取list缓存的长度
  350. *
  351. * @param key 键
  352. * @return
  353. */
  354. public long lGetListSize(String key) {
  355. try {
  356. return redisTemplate.opsForList().size(key);
  357. } catch (Exception e) {
  358. e.printStackTrace();
  359. return 0;
  360. }
  361. }
  362.  
  363. /**
  364. * 通过索引 获取list中的值
  365. *
  366. * @param key 键
  367. * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
  368. * @return
  369. */
  370. public Object lGetIndex(String key, long index) {
  371. try {
  372. return redisTemplate.opsForList().index(key, index);
  373. } catch (Exception e) {
  374. e.printStackTrace();
  375. return null;
  376. }
  377. }
  378.  
  379. /**
  380. * 将list放入缓存
  381. *
  382. * @param key 键
  383. * @param value 值
  384. * @return
  385. */
  386. public boolean lSet(String key, Object value) {
  387. try {
  388. redisTemplate.opsForList().rightPush(key, value);
  389. return true;
  390. } catch (Exception e) {
  391. e.printStackTrace();
  392. return false;
  393. }
  394. }
  395.  
  396. /**
  397. * 将list放入缓存
  398. *
  399. * @param key 键
  400. * @param value 值
  401. * @param time 时间(秒)
  402. * @return
  403. */
  404. public boolean lSet(String key, Object value, long time) {
  405. try {
  406. redisTemplate.opsForList().rightPush(key, value);
  407. if (time > 0) {
  408. expire(key, time);
  409. }
  410. return true;
  411. } catch (Exception e) {
  412. e.printStackTrace();
  413. return false;
  414. }
  415. }
  416.  
  417. /**
  418. * 将list放入缓存
  419. *
  420. * @param key 键
  421. * @param value 值
  422. * @return
  423. */
  424. public boolean lSet(String key, List<Object> value) {
  425. try {
  426. redisTemplate.opsForList().rightPushAll(key, value);
  427. return true;
  428. } catch (Exception e) {
  429. e.printStackTrace();
  430. return false;
  431. }
  432. }
  433.  
  434. /**
  435. * 将list放入缓存
  436. *
  437. * @param key 键
  438. * @param value 值
  439. * @param time 时间(秒)
  440. * @return
  441. */
  442. public boolean lSet(String key, List<Object> value, long time) {
  443. try {
  444. redisTemplate.opsForList().rightPushAll(key, value);
  445. if (time > 0) {
  446. expire(key, time);
  447. }
  448. return true;
  449. } catch (Exception e) {
  450. e.printStackTrace();
  451. return false;
  452. }
  453. }
  454.  
  455. /**
  456. * 根据索引修改list中的某条数据
  457. *
  458. * @param key 键
  459. * @param index 索引
  460. * @param value 值
  461. * @return
  462. */
  463. public boolean lUpdateIndex(String key, long index, Object value) {
  464. try {
  465. redisTemplate.opsForList().set(key, index, value);
  466. return true;
  467. } catch (Exception e) {
  468. e.printStackTrace();
  469. return false;
  470. }
  471. }
  472.  
  473. /**
  474. * 移除N个值为value
  475. *
  476. * @param key 键
  477. * @param count 移除多少个
  478. * @param value 值
  479. * @return 移除的个数
  480. */
  481. public long lRemove(String key, long count, Object value) {
  482. try {
  483. Long remove = redisTemplate.opsForList().remove(key, count, value);
  484. return remove;
  485. } catch (Exception e) {
  486. e.printStackTrace();
  487. return 0;
  488. }
  489. }
  490. //============================set=============================
  491.  
  492. /**
  493. * 根据key获取Set中的所有值
  494. *
  495. * @param key 键
  496. * @return
  497. */
  498. public Set<Object> sGet(String key) {
  499. try {
  500. return redisTemplate.opsForSet().members(key);
  501. } catch (Exception e) {
  502. e.printStackTrace();
  503. return null;
  504. }
  505. }
  506.  
  507. /**
  508. * 根据value从一个set中查询,是否存在
  509. *
  510. * @param key 键
  511. * @param value 值
  512. * @return true 存在 false不存在
  513. */
  514. public boolean sHasKey(String key, Object value) {
  515. try {
  516. return redisTemplate.opsForSet().isMember(key, value);
  517. } catch (Exception e) {
  518. e.printStackTrace();
  519. return false;
  520. }
  521. }
  522.  
  523. /**
  524. * 将数据放入set缓存
  525. *
  526. * @param key 键
  527. * @param values 值 可以是多个
  528. * @return 成功个数
  529. */
  530. public long sSet(String key, Object... values) {
  531. try {
  532. return redisTemplate.opsForSet().add(key, values);
  533. } catch (Exception e) {
  534. e.printStackTrace();
  535. return 0;
  536. }
  537. }
  538.  
  539. /**
  540. * 将set数据放入缓存
  541. *
  542. * @param key 键
  543. * @param time 时间(秒)
  544. * @param values 值 可以是多个
  545. * @return 成功个数
  546. */
  547. public long sSetAndTime(String key, long time, Object... values) {
  548. try {
  549. Long count = redisTemplate.opsForSet().add(key, values);
  550. if (time > 0) {
  551. expire(key, time);
  552. }
  553. return count;
  554. } catch (Exception e) {
  555. e.printStackTrace();
  556. return 0;
  557. }
  558. }
  559.  
  560. /**
  561. * 获取set缓存的长度
  562. *
  563. * @param key 键
  564. * @return
  565. */
  566. public long sGetSetSize(String key) {
  567. try {
  568. return redisTemplate.opsForSet().size(key);
  569. } catch (Exception e) {
  570. e.printStackTrace();
  571. return 0;
  572. }
  573. }
  574.  
  575. /**
  576. * 移除值为value的
  577. *
  578. * @param key 键
  579. * @param values 值 可以是多个
  580. * @return 移除的个数
  581. */
  582. public long setRemove(String key, Object... values) {
  583. try {
  584. Long count = redisTemplate.opsForSet().remove(key, values);
  585. return count;
  586. } catch (Exception e) {
  587. e.printStackTrace();
  588. return 0;
  589. }
  590. }
  591.  
  592. //====================================================zSet 有序集合=======================
  593.  
  594. /**
  595. * 添加元素 ,参数分别是 键 ,分数 ,字段名
  596. */
  597. //
  598. public Boolean zadd(String key, double score, String member) {
  599. // System.out.println(key);
  600. // System.out.println(score);
  601. // System.out.println(member);
  602. return redisTemplate.opsForZSet().add(key, member, score);
  603. }
  604.  
  605. /**
  606. * 获取元素数量
  607. */
  608. public long zcard(String key) {
  609. try {
  610. return redisTemplate.opsForZSet().zCard(key);
  611. } catch (Exception e) {
  612. e.printStackTrace();
  613. return 0;
  614. }
  615. }
  616.  
  617. /**
  618. * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容 zrange
  619. * <p>
  620. * 返回有序的集合,score小的在前面
  621. */
  622. public Set<Object> zrange(String key, long start, long end) {
  623. return redisTemplate.opsForZSet().range(key, start, end);
  624. }
  625.  
  626. /**
  627. * 删除某个或多个字段
  628. */
  629. public long zrem(String key, Object... members) {
  630. try {
  631. return redisTemplate.opsForZSet().remove(key, members);
  632. } catch (Exception e) {
  633. e.printStackTrace();
  634. return 0;
  635. }
  636.  
  637. }
  638.  
  639. /**
  640. * 查询value对应的score zscore
  641. * <p>
  642. * 当value在集合中时,返回其score;如果不在,则返回null
  643. */
  644. public Double score(String key, String value) {
  645. return redisTemplate.opsForZSet().score(key, value);
  646. }
  647.  
  648. /**
  649. * 判断value在zset中的排名 zrank
  650. * <p>
  651. * 这里score越小排名越高;
  652. */
  653. public long rank(String key, String value) {
  654. try {
  655. return redisTemplate.opsForZSet().rank(key, value);
  656. } catch (
  657. Exception e) {
  658. e.printStackTrace();
  659. return 0;
  660. }
  661. }
  662.  
  663. /**
  664. * score的增加or减少 zincrby
  665. */
  666. public Double incrScore(String key, String value, double score) {
  667. return redisTemplate.opsForZSet().incrementScore(key, value, score);
  668. }
  669.  
  670. /**
  671. * 返回集合的长度
  672. */
  673. public long size(String key) {
  674. try {
  675. return redisTemplate.opsForZSet().zCard(key);
  676. } catch (Exception e) {
  677. e.printStackTrace();
  678. return 0;
  679. }
  680. }
  681.  
  682. /**
  683. * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容
  684. */
  685. public Set<ZSetOperations.TypedTuple<Object>> rangeWithScore(String key, int start, int end) {
  686. return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
  687. }
  688.  
  689. /**
  690. * 查询集合中指定顺序的值 zrevrange
  691. * <p>
  692. * 返回有序的集合中,score大的在前面
  693. */
  694. public Set<Object> revRange(String key, int start, int end) {
  695. return redisTemplate.opsForZSet().reverseRange(key, start, end);
  696. }
  697.  
  698. /**
  699. * 根据score的值,来获取满足条件的集合 zrangebyscore
  700. */
  701. public Set<Object> sortRange(String key, int min, int max) {
  702. return redisTemplate.opsForZSet().rangeByScore(key, min, max);
  703. }
  704.  
  705. }

【补充springMVC的封装工具,可参考使用

  1. package cn.cen2guo.clinic.redis;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. import redis.clients.jedis.Jedis;
  8. import redis.clients.jedis.JedisPool;
  9. import redis.clients.jedis.SortingParams;
  10.  
  11. import redis.clients.jedis.BinaryClient.LIST_POSITION;
  12. import redis.clients.util.SafeEncoder;
  13.  
  14. /*
  15. 下面这两个在高版本的redis.clients依赖包里面没有,需要降低版本才有,如2.9.0
  16. *import redis.clients.jedis.BinaryClient.LIST_POSITION;
  17. import redis.clients.util.SafeEncoder;
  18. */
  19.  
  20. public class JedisUtil {
  21. /**
  22. * 缓存生存时间
  23. */
  24. private final int expire = 60000;
  25. /**
  26. * 操作Key的方法
  27. */
  28. public Keys KEYS;
  29. /**
  30. * 对存储结构为Set类型的操作
  31. */
  32. public Sets SETS;
  33. /**
  34. * 对存储结构为HashMap类型的操作
  35. */
  36. public Hash HASH;
  37. /**
  38. * 对存储结构为String类型的操作
  39. */
  40. public Strings STRINGS;
  41. /**
  42. * 对存储结构为List类型的操作
  43. */
  44. public Lists LISTS;
  45. /*** 有序队列*/
  46. public ZSets ZSets;
  47. /**
  48. * 获取jedis对象,用于锁的使用
  49. */
  50. public Jedis MyGetJedis;
  51.  
  52. //
  53. //
  54. //
  55.  
  56. private JedisPool jedisPool;
  57.  
  58. public JedisPool getJedisPool() {
  59. return jedisPool;
  60. }
  61.  
  62. public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
  63. this.jedisPool = jedisPoolWriper.getJedisPool();
  64. }
  65.  
  66. public JedisPool getPool() {
  67. return jedisPool;
  68. }
  69.  
  70. /**
  71. * 从jedis连接池中获取获取jedis对象
  72. *
  73. * @return
  74. */
  75. public Jedis getJedis() {
  76. return jedisPool.getResource();
  77. }
  78.  
  79. /**
  80. * 设置过期时间
  81. *
  82. * @param key
  83. * @param seconds
  84. * @author ruan 2013-4-11
  85. */
  86. public void expire(String key, int seconds) {
  87. if (seconds <= 0) {
  88. return;
  89. }
  90. Jedis jedis = getJedis();
  91. jedis.expire(key, seconds);
  92. jedis.close();
  93. }
  94.  
  95. /**
  96. * 设置默认过期时间
  97. *
  98. * @param key
  99. * @author ruan 2013-4-11
  100. */
  101. public void expire(String key) {
  102. expire(key, expire);
  103. }
  104.  
  105. // *******************************************Keys*******************************************//
  106. public class Keys {
  107.  
  108. public Keys(JedisUtil jedisUtil) {
  109.  
  110. }
  111.  
  112. /**
  113. * 清空所有key
  114. */
  115. public String flushAll() {
  116. Jedis jedis = getJedis();
  117. String stata = jedis.flushAll();
  118. jedis.close();
  119. return stata;
  120. }
  121.  
  122. /**
  123. * 更改key
  124. * <p>
  125. * // * @param String
  126. * oldkey
  127. * // * @param String
  128. * newkey
  129. *
  130. * @return 状态码
  131. */
  132. public String rename(String oldkey, String newkey) {
  133. return rename(SafeEncoder.encode(oldkey),
  134. SafeEncoder.encode(newkey));
  135. }
  136.  
  137. /**
  138. * 更改key,仅当新key不存在时才执行
  139. * <p>
  140. * // * @param String
  141. * oldkey
  142. * // * @param String
  143. * newkey
  144. *
  145. * @return 状态码
  146. */
  147. public long renamenx(String oldkey, String newkey) {
  148. Jedis jedis = getJedis();
  149. long status = jedis.renamenx(oldkey, newkey);
  150. jedis.close();
  151. return status;
  152. }
  153.  
  154. /**
  155. * 更改key
  156. * <p>
  157. * // * @param String
  158. * oldkey
  159. * // * @param String
  160. * newkey
  161. *
  162. * @return 状态码
  163. */
  164. public String rename(byte[] oldkey, byte[] newkey) {
  165. Jedis jedis = getJedis();
  166. String status = jedis.rename(oldkey, newkey);
  167. jedis.close();
  168. return status;
  169. }
  170.  
  171. /**
  172. * 设置key的过期时间,以秒为单位
  173. * <p>
  174. * // * @param String
  175. * key
  176. * // * @param 时间
  177. * ,已秒为单位
  178. *
  179. * @return 影响的记录数
  180. */
  181. public long expired(String key, int seconds) {
  182. Jedis jedis = getJedis();
  183. long count = jedis.expire(key, seconds);
  184. jedis.close();
  185. return count;
  186. }
  187.  
  188. /**
  189. * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
  190. * <p>
  191. * // * @param String
  192. * key
  193. * // * @param 时间
  194. * ,已秒为单位
  195. *
  196. * @return 影响的记录数
  197. */
  198. public long expireAt(String key, long timestamp) {
  199. Jedis jedis = getJedis();
  200. long count = jedis.expireAt(key, timestamp);
  201. jedis.close();
  202. return count;
  203. }
  204.  
  205. /**
  206. * 查询key的过期时间
  207. * <p>
  208. * // * @param String
  209. * key
  210. *
  211. * @return 以秒为单位的时间表示
  212. */
  213. public long ttl(String key) {
  214. // ShardedJedis sjedis = getShardedJedis();
  215. Jedis sjedis = getJedis();
  216. long len = sjedis.ttl(key);
  217. sjedis.close();
  218. return len;
  219. }
  220.  
  221. /**
  222. * 取消对key过期时间的设置
  223. *
  224. * @param key
  225. * @return 影响的记录数
  226. */
  227. public long persist(String key) {
  228. Jedis jedis = getJedis();
  229. long count = jedis.persist(key);
  230. jedis.close();
  231. return count;
  232. }
  233.  
  234. /**
  235. * 删除keys对应的记录,可以是多个key
  236. * <p>
  237. * // * @param String
  238. * ... keys
  239. *
  240. * @return 删除的记录数
  241. */
  242. public long del(String... keys) {
  243. Jedis jedis = getJedis();
  244. long count = jedis.del(keys);
  245. jedis.close();
  246. return count;
  247. }
  248.  
  249. // String…是java5新加入的功能,表示的是一个可变长度的参数列表。
  250. // 其语法就是类型后跟…,表示此处接受的参数为0到多个Object类型的对象,或者是一个Object[]。
  251. // 例如我们有一个方法叫做test(String…strings),那么你还可以写方法test(),但你不能写test(String[] strings),
  252. // 这样会出编译错误,系统提示出现重复的方法。
  253. // 在使用的时候,对于test(String…strings),你可以直接用test()去调用,标示没有参数,也可以用去test(“aaa”),
  254. // 也可以用test(new String[]{“aaa”,”bbb”})。
  255. // 另外如果既有test(String…strings)函数,又有test()函数,我们在调用test()时,会优先使用test()函数。
  256. // 只有当没有test()函数式,我们调用test(),程序才会走test(String…strings)。
  257.  
  258. /**
  259. * 删除keys对应的记录,可以是多个key
  260. * <p>
  261. * // * @param String
  262. * ... keys
  263. *
  264. * @return 删除的记录数
  265. */
  266. public long del(byte[]... keys) {
  267. Jedis jedis = getJedis();
  268. long count = jedis.del(keys);
  269. jedis.close();
  270. return count;
  271. }
  272.  
  273. /**
  274. * 判断key是否存在
  275. * <p>
  276. * // * @param String
  277. * key
  278. *
  279. * @return boolean
  280. */
  281. public boolean exists(String key) {
  282. // ShardedJedis sjedis = getShardedJedis();
  283. Jedis sjedis = getJedis();
  284. boolean exis = sjedis.exists(key);
  285. sjedis.close();
  286. return exis;
  287. }
  288.  
  289. /**
  290. * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
  291. * <p>
  292. * // * @param String
  293. * key
  294. *
  295. * @return List<String> 集合的全部记录
  296. **/
  297. public List<String> sort(String key) {
  298. // ShardedJedis sjedis = getShardedJedis();
  299. Jedis sjedis = getJedis();
  300. List<String> list = sjedis.sort(key);
  301. sjedis.close();
  302. return list;
  303. }
  304.  
  305. /**
  306. * 对List,Set,SortSet进行排序或limit
  307. * <p>
  308. * // * @param String
  309. * key
  310. * // * @param SortingParams
  311. * parame 定义排序类型或limit的起止位置.
  312. *
  313. * @return List<String> 全部或部分记录
  314. **/
  315. public List<String> sort(String key, SortingParams parame) {
  316. // ShardedJedis sjedis = getShardedJedis();
  317. Jedis sjedis = getJedis();
  318. List<String> list = sjedis.sort(key, parame);
  319. sjedis.close();
  320. return list;
  321. }
  322.  
  323. /**
  324. * 返回指定key存储的类型
  325. * <p>
  326. * // * @param String
  327. * key
  328. *
  329. * @return String string|list|set|zset|hash
  330. **/
  331. public String type(String key) {
  332. // ShardedJedis sjedis = getShardedJedis();
  333. Jedis sjedis = getJedis();
  334. String type = sjedis.type(key);
  335. sjedis.close();
  336. return type;
  337. }
  338.  
  339. /**
  340. * 查找所有匹配给定的模式的键
  341. * <p>
  342. * // * @param String
  343. * key的表达式,*表示多个,?表示一个
  344. */
  345. public Set<String> keys(String pattern) {
  346. Jedis jedis = getJedis();
  347. Set<String> set = jedis.keys(pattern);
  348. jedis.close();
  349. return set;
  350. }
  351. }
  352.  
  353. // *******************************************Sets*******************************************//
  354. public class Sets {
  355.  
  356. public Sets(JedisUtil jedisUtil) {
  357.  
  358. }
  359.  
  360. /**
  361. * 向Set添加一条记录,如果member已存在返回0,否则返回1
  362. * <p>
  363. * // * @param String
  364. * key
  365. * // * @param String
  366. * member
  367. *
  368. * @return 操作码, 0或1
  369. */
  370. public long sadd(String key, String member) {
  371. Jedis jedis = getJedis();
  372. long s = jedis.sadd(key, member);
  373. jedis.close();
  374. return s;
  375. }
  376.  
  377. public long sadd(byte[] key, byte[] member) {
  378. Jedis jedis = getJedis();
  379. long s = jedis.sadd(key, member);
  380. jedis.close();
  381. return s;
  382. }
  383.  
  384. /**
  385. * 获取给定key中元素个数
  386. * <p>
  387. * // * @param String
  388. * key
  389. *
  390. * @return 元素个数
  391. */
  392. public long scard(String key) {
  393. // ShardedJedis sjedis = getShardedJedis();
  394. Jedis sjedis = getJedis();
  395. long len = sjedis.scard(key);
  396. sjedis.close();
  397. return len;
  398. }
  399.  
  400. /**
  401. * 返回从第一组和所有的给定集合之间的差异的成员
  402. * <p>
  403. * // * @param String
  404. * ... keys
  405. *
  406. * @return 差异的成员集合
  407. */
  408. public Set<String> sdiff(String... keys) {
  409. Jedis jedis = getJedis();
  410. Set<String> set = jedis.sdiff(keys);
  411. jedis.close();
  412. return set;
  413. }
  414.  
  415. /**
  416. * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
  417. * <p>
  418. * // * @param String
  419. * newkey 新结果集的key
  420. * // * @param String
  421. * ... keys 比较的集合
  422. *
  423. * @return 新集合中的记录数
  424. **/
  425. public long sdiffstore(String newkey, String... keys) {
  426. Jedis jedis = getJedis();
  427. long s = jedis.sdiffstore(newkey, keys);
  428. jedis.close();
  429. return s;
  430. }
  431.  
  432. /**
  433. * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
  434. * <p>
  435. * // * @param String
  436. * ... keys
  437. *
  438. * @return 交集成员的集合
  439. **/
  440. public Set<String> sinter(String... keys) {
  441. Jedis jedis = getJedis();
  442. Set<String> set = jedis.sinter(keys);
  443. jedis.close();
  444. return set;
  445. }
  446.  
  447. /**
  448. * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
  449. * <p>
  450. * // * @param String
  451. * newkey 新结果集的key
  452. * // * @param String
  453. * ... keys 比较的集合
  454. *
  455. * @return 新集合中的记录数
  456. **/
  457. public long sinterstore(String newkey, String... keys) {
  458. Jedis jedis = getJedis();
  459. long s = jedis.sinterstore(newkey, keys);
  460. jedis.close();
  461. return s;
  462. }
  463.  
  464. /**
  465. * 确定一个给定的值是否存在
  466. * <p>
  467. * // * @param String
  468. * key
  469. * // * @param String
  470. * member 要判断的值
  471. *
  472. * @return 存在返回1,不存在返回0
  473. **/
  474. public boolean sismember(String key, String member) {
  475. // ShardedJedis sjedis = getShardedJedis();
  476. Jedis sjedis = getJedis();
  477. boolean s = sjedis.sismember(key, member);
  478. sjedis.close();
  479. return s;
  480. }
  481.  
  482. /**
  483. * 返回集合中的所有成员
  484. * <p>
  485. * // * @param String
  486. * key
  487. *
  488. * @return 成员集合
  489. */
  490. public Set<String> smembers(String key) {
  491. // ShardedJedis sjedis = getShardedJedis();
  492. Jedis sjedis = getJedis();
  493. Set<String> set = sjedis.smembers(key);
  494. sjedis.close();
  495. return set;
  496. }
  497.  
  498. public Set<byte[]> smembers(byte[] key) {
  499. // ShardedJedis sjedis = getShardedJedis();
  500. Jedis sjedis = getJedis();
  501. Set<byte[]> set = sjedis.smembers(key);
  502. sjedis.close();
  503. return set;
  504. }
  505.  
  506. /**
  507. * 将成员从源集合移出放入目标集合 <br/>
  508. * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
  509. * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
  510. * <p>
  511. * // * @param String
  512. * srckey 源集合
  513. * // * @param String
  514. * dstkey 目标集合
  515. * // * @param String
  516. * member 源集合中的成员
  517. *
  518. * @return 状态码,1成功,0失败
  519. */
  520. public long smove(String srckey, String dstkey, String member) {
  521. Jedis jedis = getJedis();
  522. long s = jedis.smove(srckey, dstkey, member);
  523. jedis.close();
  524. return s;
  525. }
  526.  
  527. /**
  528. * 从集合中删除成员
  529. * <p>
  530. * // * @param String
  531. * key
  532. *
  533. * @return 被删除的成员
  534. */
  535. public String spop(String key) {
  536. Jedis jedis = getJedis();
  537. String s = jedis.spop(key);
  538. jedis.close();
  539. return s;
  540. }
  541.  
  542. /**
  543. * 从集合中删除指定成员
  544. * <p>
  545. * // * @param String
  546. * key
  547. * // * @param String
  548. * member 要删除的成员
  549. *
  550. * @return 状态码,成功返回1,成员不存在返回0
  551. */
  552. public long srem(String key, String member) {
  553. Jedis jedis = getJedis();
  554. long s = jedis.srem(key, member);
  555. jedis.close();
  556. return s;
  557. }
  558.  
  559. /**
  560. * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
  561. * <p>
  562. * // * @param String
  563. * ... keys
  564. *
  565. * @return 合并后的结果集合
  566. * // * @see sunionstore
  567. */
  568. public Set<String> sunion(String... keys) {
  569. Jedis jedis = getJedis();
  570. Set<String> set = jedis.sunion(keys);
  571. jedis.close();
  572. return set;
  573. }
  574.  
  575. /**
  576. * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
  577. * <p>
  578. * // * @param String
  579. * newkey 新集合的key
  580. * // * @param String
  581. * ... keys 要合并的集合
  582. **/
  583. public long sunionstore(String newkey, String... keys) {
  584. Jedis jedis = getJedis();
  585. long s = jedis.sunionstore(newkey, keys);
  586. jedis.close();
  587. return s;
  588. }
  589. }
  590.  
  591. // *******************************************Hash*******************************************//
  592. public class Hash {
  593.  
  594. public Hash(JedisUtil jedisUtil) {
  595.  
  596. }
  597.  
  598. /**
  599. * 从hash中删除指定的存储
  600. * <p>
  601. * // * @param String
  602. * key
  603. * // * @param String
  604. * fieid 存储的名字
  605. *
  606. * @return 状态码,1成功,0失败
  607. */
  608. public long hdel(String key, String fieid) {
  609. Jedis jedis = getJedis();
  610. long s = jedis.hdel(key, fieid);
  611. jedis.close();
  612. return s;
  613. }
  614.  
  615. public long hdel(String key) {
  616. Jedis jedis = getJedis();
  617. long s = jedis.del(key);
  618. jedis.close();
  619. return s;
  620. }
  621.  
  622. /**
  623. * 测试hash中指定的存储是否存在
  624. * <p>
  625. * // * @param String
  626. * key
  627. * // * @param String
  628. * fieid 存储的名字
  629. *
  630. * @return 1存在,0不存在
  631. */
  632. public boolean hexists(String key, String fieid) {
  633. // ShardedJedis sjedis = getShardedJedis();
  634. Jedis sjedis = getJedis();
  635. boolean s = sjedis.hexists(key, fieid);
  636. sjedis.close();
  637. return s;
  638. }
  639.  
  640. /**
  641. * 返回hash中指定存储位置的值
  642. * <p>
  643. * // * @param String
  644. * key
  645. * // * @param String
  646. * fieid 存储的名字
  647. *
  648. * @return 存储对应的值
  649. */
  650. public String hget(String key, String fieid) {
  651. // ShardedJedis sjedis = getShardedJedis();
  652. Jedis sjedis = getJedis();
  653. String s = sjedis.hget(key, fieid);
  654. sjedis.close();
  655. return s;
  656. }
  657.  
  658. public byte[] hget(byte[] key, byte[] fieid) {
  659. // ShardedJedis sjedis = getShardedJedis();
  660. Jedis sjedis = getJedis();
  661. byte[] s = sjedis.hget(key, fieid);
  662. sjedis.close();
  663. return s;
  664. }
  665.  
  666. /**
  667. * 以Map的形式返回hash中的存储和值
  668. * <p>
  669. * // * @param String
  670. * key
  671. *
  672. * @return Map<Strinig, String>
  673. */
  674. public Map<String, String> hgetAll(String key) {
  675. // ShardedJedis sjedis = getShardedJedis();
  676. Jedis sjedis = getJedis();
  677. Map<String, String> map = sjedis.hgetAll(key);
  678. sjedis.close();
  679. return map;
  680. }
  681.  
  682. /**
  683. * 添加一个对应关系
  684. * <p>
  685. * // * @param String
  686. * key
  687. * // * @param String
  688. * fieid
  689. * // * @param String
  690. * value
  691. *
  692. * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
  693. **/
  694. public long hset(String key, String fieid, String value) {
  695. Jedis jedis = getJedis();
  696. long s = jedis.hset(key, fieid, value);
  697. jedis.close();
  698. return s;
  699. }
  700.  
  701. public long hset(String key, String fieid, byte[] value) {
  702. Jedis jedis = getJedis();
  703. long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
  704. jedis.close();
  705. return s;
  706. }
  707.  
  708. /**
  709. * 添加对应关系,只有在fieid不存在时才执行
  710. * <p>
  711. * // * @param String
  712. * key
  713. * // * @param String
  714. * fieid
  715. * // * @param String
  716. * value
  717. *
  718. * @return 状态码 1成功,0失败fieid已存
  719. **/
  720. public long hsetnx(String key, String fieid, String value) {
  721. Jedis jedis = getJedis();
  722. long s = jedis.hsetnx(key, fieid, value);
  723. jedis.close();
  724. return s;
  725. }
  726.  
  727. /**
  728. * 获取hash中value的集合
  729. * <p>
  730. * // * @param String
  731. * key
  732. *
  733. * @return List<String>
  734. */
  735. public List<String> hvals(String key) {
  736. // ShardedJedis sjedis = getShardedJedis();
  737. Jedis sjedis = getJedis();
  738. List<String> list = sjedis.hvals(key);
  739. sjedis.close();
  740. return list;
  741. }
  742.  
  743. /**
  744. * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
  745. * <p>
  746. * // * @param String
  747. * key
  748. * // * @param String
  749. * fieid 存储位置
  750. * // * @param String
  751. * long value 要增加的值,可以是负数
  752. *
  753. * @return 增加指定数字后,存储位置的值
  754. */
  755. public long hincrby(String key, String fieid, long value) {
  756. Jedis jedis = getJedis();
  757. long s = jedis.hincrBy(key, fieid, value);
  758. jedis.close();
  759. return s;
  760. }
  761.  
  762. /**
  763. * 返回指定hash中的所有存储名字,类似Map中的keySet方法
  764. * <p>
  765. * // * @param String
  766. * key
  767. *
  768. * @return Set<String> 存储名称的集合
  769. */
  770. public Set<String> hkeys(String key) {
  771. // ShardedJedis sjedis = getShardedJedis();
  772. Jedis sjedis = getJedis();
  773. Set<String> set = sjedis.hkeys(key);
  774. sjedis.close();
  775. return set;
  776. }
  777.  
  778. /**
  779. * 获取hash中存储的个数,类似Map中size方法
  780. * <p>
  781. * // * @param String
  782. * key
  783. *
  784. * @return long 存储的个数
  785. */
  786. public long hlen(String key) {
  787. // ShardedJedis sjedis = getShardedJedis();
  788. Jedis sjedis = getJedis();
  789. long len = sjedis.hlen(key);
  790. sjedis.close();
  791. return len;
  792. }
  793.  
  794. /**
  795. * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
  796. * <p>
  797. * // * @param String
  798. * key
  799. * // * @param String
  800. * ... fieids 存储位置
  801. *
  802. * @return List<String>
  803. */
  804. public List<String> hmget(String key, String... fieids) {
  805. // ShardedJedis sjedis = getShardedJedis();
  806. Jedis sjedis = getJedis();
  807. List<String> list = sjedis.hmget(key, fieids);
  808. sjedis.close();
  809. return list;
  810. }
  811.  
  812. public List<byte[]> hmget(byte[] key, byte[]... fieids) {
  813. // ShardedJedis sjedis = getShardedJedis();
  814. Jedis sjedis = getJedis();
  815. List<byte[]> list = sjedis.hmget(key, fieids);
  816. sjedis.close();
  817. return list;
  818. }
  819.  
  820. /**
  821. * 添加对应关系,如果对应关系已存在,则覆盖
  822. * <p>
  823. * // * @param Strin
  824. * key
  825. * // * @param Map
  826. * <String,String> 对应关系
  827. *
  828. * @return 状态,成功返回OK
  829. */
  830. public String hmset(String key, Map<String, String> map) {
  831. Jedis jedis = getJedis();
  832. String s = jedis.hmset(key, map);
  833. jedis.close();
  834. return s;
  835. }
  836.  
  837. /**
  838. * 添加对应关系,如果对应关系已存在,则覆盖
  839. * <p>
  840. * // * @param Strin
  841. * key
  842. * // * @param Map
  843. * <String,String> 对应关系
  844. *
  845. * @return 状态,成功返回OK
  846. */
  847. public String hmset(byte[] key, Map<byte[], byte[]> map) {
  848. Jedis jedis = getJedis();
  849. String s = jedis.hmset(key, map);
  850. jedis.close();
  851. return s;
  852. }
  853.  
  854. }
  855.  
  856. // *******************************************Strings*******************************************//
  857. public class Strings {
  858.  
  859. public Strings(JedisUtil jedisUtil) {
  860.  
  861. }
  862.  
  863. /**
  864. * 根据key获取记录
  865. * <p>
  866. * // * @param String
  867. * key
  868. *
  869. * @return 值
  870. */
  871. public String get(String key) {
  872. // ShardedJedis sjedis = getShardedJedis();
  873. Jedis sjedis = getJedis();
  874. String value = sjedis.get(key);
  875. sjedis.close();
  876. return value;
  877. }
  878.  
  879. /**
  880. * 根据key获取记录
  881. * <p>
  882. * // * @param byte[] key
  883. *
  884. * @return 值
  885. */
  886. public byte[] get(byte[] key) {
  887. // ShardedJedis sjedis = getShardedJedis();
  888. Jedis sjedis = getJedis();
  889. byte[] value = sjedis.get(key);
  890. sjedis.close();
  891. return value;
  892. }
  893.  
  894. /**
  895. * 添加有过期时间的记录
  896. * <p>
  897. * // * @param String
  898. * key
  899. * // * @param int seconds 过期时间,以秒为单位
  900. * // * @param String
  901. * value
  902. *
  903. * @return String 操作状态
  904. */
  905. public String setEx(String key, int seconds, String value) {
  906. Jedis jedis = getJedis();
  907. String str = jedis.setex(key, seconds, value);
  908. jedis.close();
  909. return str;
  910. }
  911.  
  912. /**
  913. * 添加有过期时间的记录
  914. * <p>
  915. * // * @param String
  916. * key
  917. * // * @param int seconds 过期时间,以秒为单位
  918. * // * @param String
  919. * value
  920. *
  921. * @return String 操作状态
  922. */
  923. public String setEx(byte[] key, int seconds, byte[] value) {
  924. Jedis jedis = getJedis();
  925. String str = jedis.setex(key, seconds, value);
  926. jedis.close();
  927. return str;
  928. }
  929.  
  930. /**
  931. * 添加一条记录,仅当给定的key不存在时才插入
  932. * <p>
  933. * // * @param String
  934. * key
  935. * // * @param String
  936. * value
  937. *
  938. * @return long 状态码,1插入成功且key不存在,0未插入,key存在
  939. */
  940. public long setnx(String key, String value) {
  941. Jedis jedis = getJedis();
  942. long str = jedis.setnx(key, value);
  943. jedis.close();
  944. return str;
  945. }
  946.  
  947. /**
  948. * 添加记录,如果记录已存在将覆盖原有的value
  949. * <p>
  950. * // * @param String
  951. * key
  952. * // * @param String
  953. * value
  954. *
  955. * @return 状态码
  956. */
  957. public String set(String key, String value) {
  958. return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
  959. }
  960.  
  961. /**
  962. * 添加记录,如果记录已存在将覆盖原有的value
  963. * <p>
  964. * // * @param String
  965. * key
  966. * // * @param String
  967. * value
  968. *
  969. * @return 状态码
  970. */
  971. public String set(String key, byte[] value) {
  972. return set(SafeEncoder.encode(key), value);
  973. }
  974.  
  975. /**
  976. * 添加记录,如果记录已存在将覆盖原有的value
  977. * <p>
  978. * // * @param byte[] key
  979. * // * @param byte[] value
  980. *
  981. * @return 状态码
  982. */
  983. public String set(byte[] key, byte[] value) {
  984. Jedis jedis = getJedis();
  985. String status = jedis.set(key, value);
  986. jedis.close();
  987. return status;
  988. }
  989.  
  990. /**
  991. * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
  992. * 例:String str1="123456789";<br/>
  993. * 对str1操作后setRange(key,4,0000),str1="123400009";
  994. * <p>
  995. * // * @param String
  996. * // * key
  997. * // * @param long offset
  998. * // * @param String
  999. * value
  1000. *
  1001. * @return long value的长度
  1002. */
  1003. public long setRange(String key, long offset, String value) {
  1004. Jedis jedis = getJedis();
  1005. long len = jedis.setrange(key, offset, value);
  1006. jedis.close();
  1007. return len;
  1008. }
  1009.  
  1010. /**
  1011. * 在指定的key中追加value
  1012. * <p>
  1013. * // * @param String
  1014. * // * key
  1015. * // * @param String
  1016. * value
  1017. *
  1018. * @return long 追加后value的长度
  1019. **/
  1020. public long append(String key, String value) {
  1021. Jedis jedis = getJedis();
  1022. long len = jedis.append(key, value);
  1023. jedis.close();
  1024. return len;
  1025. }
  1026.  
  1027. /**
  1028. * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
  1029. * <p>
  1030. * // * @param String
  1031. * // * key
  1032. * // * @param long number 要减去的值
  1033. *
  1034. * @return long 减指定值后的值
  1035. */
  1036. public long decrBy(String key, long number) {
  1037. Jedis jedis = getJedis();
  1038. long len = jedis.decrBy(key, number);
  1039. jedis.close();
  1040. return len;
  1041. }
  1042.  
  1043. /**
  1044. * <b>可以作为获取唯一id的方法</b><br/>
  1045. * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
  1046. * <p>
  1047. * // * @param String
  1048. * // * key
  1049. * // * @param long number 要减去的值
  1050. *
  1051. * @return long 相加后的值
  1052. */
  1053. public long incrBy(String key, long number) {
  1054. Jedis jedis = getJedis();
  1055. long len = jedis.incrBy(key, number);
  1056. jedis.close();
  1057. return len;
  1058. }
  1059.  
  1060. /**
  1061. * 对指定key对应的value进行截取
  1062. * <p>
  1063. * // * @param String
  1064. * // * key
  1065. * // * @param long startOffset 开始位置(包含)
  1066. * // * @param long endOffset 结束位置(包含)
  1067. *
  1068. * @return String 截取的值
  1069. */
  1070. public String getrange(String key, long startOffset, long endOffset) {
  1071. // ShardedJedis sjedis = getShardedJedis();
  1072. Jedis sjedis = getJedis();
  1073. String value = sjedis.getrange(key, startOffset, endOffset);
  1074. sjedis.close();
  1075. return value;
  1076. }
  1077.  
  1078. /**
  1079. * 获取并设置指定key对应的value<br/>
  1080. * 如果key存在返回之前的value,否则返回null
  1081. * <p>
  1082. * // * @param String
  1083. * // * key
  1084. * // * @param String
  1085. * value
  1086. *
  1087. * @return String 原始value或null
  1088. */
  1089. public String getSet(String key, String value) {
  1090. Jedis jedis = getJedis();
  1091. String str = jedis.getSet(key, value);
  1092. jedis.close();
  1093. return str;
  1094. }
  1095.  
  1096. /**
  1097. * 批量获取记录,如果指定的key不存在返回List的对应位置将是null
  1098. * <p>
  1099. * // * @param String
  1100. * keys
  1101. *
  1102. * @return List<String> 值得集合
  1103. */
  1104. public List<String> mget(String... keys) {
  1105. Jedis jedis = getJedis();
  1106. List<String> str = jedis.mget(keys);
  1107. jedis.close();
  1108. return str;
  1109. }
  1110.  
  1111. /**
  1112. * 批量存储记录
  1113. * <p>
  1114. * // * @param String
  1115. * keysvalues 例:keysvalues="key1","value1","key2","value2";
  1116. *
  1117. * @return String 状态码
  1118. */
  1119. public String mset(String... keysvalues) {
  1120. Jedis jedis = getJedis();
  1121. String str = jedis.mset(keysvalues);
  1122. jedis.close();
  1123. return str;
  1124. }
  1125.  
  1126. /**
  1127. * 获取key对应的值的长度
  1128. * <p>
  1129. * // * @param String
  1130. * key
  1131. *
  1132. * @return value值得长度
  1133. */
  1134. public long strlen(String key) {
  1135. Jedis jedis = getJedis();
  1136. long len = jedis.strlen(key);
  1137. jedis.close();
  1138. return len;
  1139. }
  1140. }
  1141.  
  1142. // *******************************************Lists*******************************************//
  1143. public class Lists {
  1144.  
  1145. public Lists(JedisUtil jedisUtil) {
  1146.  
  1147. }
  1148.  
  1149. /**
  1150. * List长度
  1151. * <p>
  1152. * // * @param String
  1153. * key
  1154. *
  1155. * @return 长度
  1156. */
  1157. public long llen(String key) {
  1158. return llen(SafeEncoder.encode(key));
  1159. }
  1160.  
  1161. /**
  1162. * List长度
  1163. * <p>
  1164. * // * @param byte[] key
  1165. *
  1166. * @return 长度
  1167. */
  1168. public long llen(byte[] key) {
  1169. // ShardedJedis sjedis = getShardedJedis();
  1170. Jedis sjedis = getJedis();
  1171. long count = sjedis.llen(key);
  1172. sjedis.close();
  1173. return count;
  1174. }
  1175.  
  1176. /**
  1177. * 覆盖操作,将覆盖List中指定位置的值
  1178. * <p>
  1179. * // * @param byte[] key
  1180. * // * @param int index 位置
  1181. * // * @param byte[] value 值
  1182. *
  1183. * @return 状态码
  1184. */
  1185. public String lset(byte[] key, int index, byte[] value) {
  1186. Jedis jedis = getJedis();
  1187. String status = jedis.lset(key, index, value);
  1188. jedis.close();
  1189. return status;
  1190. }
  1191.  
  1192. /**
  1193. * 覆盖操作,将覆盖List中指定位置的值
  1194. * <p>
  1195. * // * @param key
  1196. * // * @param int index 位置
  1197. * // * @param String
  1198. * value 值
  1199. *
  1200. * @return 状态码
  1201. */
  1202. public String lset(String key, int index, String value) {
  1203. return lset(SafeEncoder.encode(key), index,
  1204. SafeEncoder.encode(value));
  1205. }
  1206.  
  1207. /**
  1208. * 在value的相对位置插入记录
  1209. * <p>
  1210. * // * @param key
  1211. * // * @param LIST_POSITION
  1212. * // * 前面插入或后面插入
  1213. * // * @param String
  1214. * // * pivot 相对位置的内容
  1215. * // * @param String
  1216. * // * value 插入的内容
  1217. *
  1218. * @return 记录总数
  1219. */
  1220. public long linsert(String key, LIST_POSITION where, String pivot,
  1221. String value) {
  1222. return linsert(SafeEncoder.encode(key), where,
  1223. SafeEncoder.encode(pivot), SafeEncoder.encode(value));
  1224. }
  1225.  
  1226. /**
  1227. * 在指定位置插入记录
  1228. * <p>
  1229. * // * @param String
  1230. * // * key
  1231. * // * @param LIST_POSITION
  1232. * // * 前面插入或后面插入
  1233. * // * @param byte[] pivot 相对位置的内容
  1234. * // * @param byte[] value 插入的内容
  1235. *
  1236. * @return 记录总数
  1237. */
  1238. public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,
  1239. byte[] value) {
  1240. Jedis jedis = getJedis();
  1241. long count = jedis.linsert(key, where, pivot, value);
  1242. jedis.close();
  1243. return count;
  1244. }
  1245.  
  1246. /**
  1247. * 获取List中指定位置的值
  1248. * <p>
  1249. * // * @param String
  1250. * // * key
  1251. * // * @param int index 位置
  1252. *
  1253. * @return 值
  1254. **/
  1255. public String lindex(String key, int index) {
  1256. return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
  1257. }
  1258.  
  1259. /**
  1260. * 获取List中指定位置的值
  1261. * <p>
  1262. * // * @param byte[] key
  1263. * // * @param int index 位置
  1264. * // * @return 值
  1265. **/
  1266. public byte[] lindex(byte[] key, int index) {
  1267. // ShardedJedis sjedis = getShardedJedis();
  1268. Jedis sjedis = getJedis();
  1269. byte[] value = sjedis.lindex(key, index);
  1270. sjedis.close();
  1271. return value;
  1272. }
  1273.  
  1274. /**
  1275. * 将List中的第一条记录移出List
  1276. * <p>
  1277. * // * @param String
  1278. * key
  1279. *
  1280. * @return 移出的记录
  1281. */
  1282. public String lpop(String key) {
  1283. return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
  1284. }
  1285.  
  1286. /**
  1287. * 将List中的第一条记录移出List
  1288. * <p>
  1289. * // * @param byte[] key
  1290. *
  1291. * @return 移出的记录
  1292. */
  1293. public byte[] lpop(byte[] key) {
  1294. Jedis jedis = getJedis();
  1295. byte[] value = jedis.lpop(key);
  1296. jedis.close();
  1297. return value;
  1298. }
  1299.  
  1300. /**
  1301. * 将List中最后第一条记录移出List
  1302. * <p>
  1303. * // * @param byte[] key
  1304. *
  1305. * @return 移出的记录
  1306. */
  1307. public String rpop(String key) {
  1308. Jedis jedis = getJedis();
  1309. String value = jedis.rpop(key);
  1310. jedis.close();
  1311. return value;
  1312. }
  1313.  
  1314. /**
  1315. * 向List尾部追加记录
  1316. * <p>
  1317. * // * @param String
  1318. * // * key
  1319. * // * @param String
  1320. * value
  1321. *
  1322. * @return 记录总数
  1323. */
  1324. public long lpush(String key, String value) {
  1325. return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
  1326. }
  1327.  
  1328. /**
  1329. * 向List头部追加记录
  1330. * <p>
  1331. * // * @param String
  1332. * // * key
  1333. * // * @param String
  1334. * value
  1335. *
  1336. * @return 记录总数
  1337. */
  1338. public long rpush(String key, String value) {
  1339. Jedis jedis = getJedis();
  1340. long count = jedis.rpush(key, value);
  1341. jedis.close();
  1342. return count;
  1343. }
  1344.  
  1345. /**
  1346. * 向List头部追加记录
  1347. * <p>
  1348. * // * @param String
  1349. * // * key
  1350. * // * @param String
  1351. * value
  1352. *
  1353. * @return 记录总数
  1354. */
  1355. public long rpush(byte[] key, byte[] value) {
  1356. Jedis jedis = getJedis();
  1357. long count = jedis.rpush(key, value);
  1358. jedis.close();
  1359. return count;
  1360. }
  1361.  
  1362. /**
  1363. * 向List中追加记录
  1364. * <p>
  1365. * // * @param byte[] key
  1366. * // * @param byte[] value
  1367. *
  1368. * @return 记录总数
  1369. */
  1370. public long lpush(byte[] key, byte[] value) {
  1371. Jedis jedis = getJedis();
  1372. long count = jedis.lpush(key, value);
  1373. jedis.close();
  1374. return count;
  1375. }
  1376.  
  1377. /**
  1378. * 获取指定范围的记录,可以做为分页使用
  1379. * <p>
  1380. * // * @param String
  1381. * // * key
  1382. * // * @param long start
  1383. * // * @param long end
  1384. *
  1385. * @return List
  1386. */
  1387. public List<String> lrange(String key, long start, long end) {
  1388. // ShardedJedis sjedis = getShardedJedis();
  1389. Jedis sjedis = getJedis();
  1390. List<String> list = sjedis.lrange(key, start, end);
  1391. sjedis.close();
  1392. return list;
  1393. }
  1394.  
  1395. /**
  1396. * 获取指定范围的记录,可以做为分页使用
  1397. * <p>
  1398. * // * @param byte[] key
  1399. * // * @param int start
  1400. * // * @param int end 如果为负数,则尾部开始计算
  1401. *
  1402. * @return List
  1403. */
  1404. public List<byte[]> lrange(byte[] key, int start, int end) {
  1405. // ShardedJedis sjedis = getShardedJedis();
  1406. Jedis sjedis = getJedis();
  1407. List<byte[]> list = sjedis.lrange(key, start, end);
  1408. sjedis.close();
  1409. return list;
  1410. }
  1411.  
  1412. /**
  1413. * 删除List中c条记录,被删除的记录值为value
  1414. * <p>
  1415. * // * @param byte[] key
  1416. * // * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
  1417. * // * @param byte[] value 要匹配的值
  1418. *
  1419. * @return 删除后的List中的记录数
  1420. */
  1421. public long lrem(byte[] key, int c, byte[] value) {
  1422. Jedis jedis = getJedis();
  1423. long count = jedis.lrem(key, c, value);
  1424. jedis.close();
  1425. return count;
  1426. }
  1427.  
  1428. /**
  1429. * 删除List中c条记录,被删除的记录值为value
  1430. * <p>
  1431. * // * @param String
  1432. * // * key
  1433. * // * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
  1434. * // * @param String
  1435. * value 要匹配的值
  1436. *
  1437. * @return 删除后的List中的记录数
  1438. */
  1439. public long lrem(String key, int c, String value) {
  1440. return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
  1441. }
  1442.  
  1443. /**
  1444. * 算是删除吧,只保留start与end之间的记录
  1445. * <p>
  1446. * // * @param byte[] key
  1447. * // * @param int start 记录的开始位置(0表示第一条记录)
  1448. * // * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
  1449. *
  1450. * @return 执行状态码
  1451. */
  1452. public String ltrim(byte[] key, int start, int end) {
  1453. Jedis jedis = getJedis();
  1454. String str = jedis.ltrim(key, start, end);
  1455. jedis.close();
  1456. return str;
  1457. }
  1458.  
  1459. /**
  1460. * 算是删除吧,只保留start与end之间的记录
  1461. * <p>
  1462. * // * @param String
  1463. * // * key
  1464. * // * @param int start 记录的开始位置(0表示第一条记录)
  1465. * // * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
  1466. *
  1467. * @return 执行状态码
  1468. */
  1469. public String ltrim(String key, int start, int end) {
  1470. return ltrim(SafeEncoder.encode(key), start, end);
  1471. }
  1472. }
  1473.  
  1474. //====================================================================================================
  1475.  
  1476. /**
  1477. * 有序集合
  1478. */
  1479. public class ZSets {
  1480.  
  1481. public ZSets(JedisUtil jedisUtil) {
  1482.  
  1483. }
  1484.  
  1485. /**
  1486. * 添加元素
  1487. */
  1488. public long zadd(String key, double score, String member) {
  1489. System.out.println(key);
  1490. System.out.println(score);
  1491. System.out.println(member);
  1492. Jedis jedis = getJedis();
  1493. long s = jedis.zadd(key,score, member);
  1494. jedis.close();
  1495. return s;
  1496. }
  1497. /**
  1498. * 获取元素数量
  1499. */
  1500. public long zcard(String key) {
  1501. // ShardedJedis sjedis = getShardedJedis();
  1502. Jedis sjedis = getJedis();
  1503. long len = sjedis.zcard(key);
  1504. sjedis.close();
  1505. return len;
  1506. }
  1507.  
  1508. /**
  1509. * 根据数值获取前 几个 的字段
  1510. */
  1511. public Set<String> zrange(String key ,long start , long end ) {
  1512. Jedis jedis = getJedis();
  1513. Set<String> set = jedis.zrange(key,start,end);
  1514. jedis.close();
  1515. return set;
  1516. }
  1517. /**
  1518. * 删除某个或多个字段
  1519. */
  1520. public long zrem(String key, String... members) {
  1521. Jedis jedis = getJedis();
  1522. long s = jedis.zrem(key, members);
  1523. jedis.close();
  1524. return s;
  1525. }
  1526.  
  1527. }
  1528.  
  1529. //====================================================================================================
  1530. /**
  1531. * 获取jedis对象,用于锁的使用
  1532. */
  1533. public class MyGetJedis{
  1534. public MyGetJedis(JedisUtil jedisUtil) {
  1535.  
  1536. }
  1537.  
  1538. /**
  1539. * 获取jedis对象,传出去
  1540. */
  1541. public Jedis myGetJedis(){
  1542. return getJedis();
  1543. }
  1544. }
  1545.  
  1546. }

(6)service层创建接口

  1. package com.example.providerredis8003.service;
  2.  
  3. import java.util.Map;
  4.  
  5. public interface EatService {
  6. public Map<String,Object> addFood(String name);
  7.  
  8. public Map<String,Object> getAll();
  9. }

(7)service层接口具体实现类

  1. package com.example.providerredis8003.service.serviceImpl;
  2.  
  3. import com.example.providerredis8003.service.EatService;
  4. import com.example.providerredis8003.util.RedisUtil;
  5.  
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import javax.annotation.Resource;
  10. import java.util.*;
  11.  
  12. @Service
  13. public class EatServiceImpl implements EatService {
  14.  
  15. @Resource
  16. private RedisUtil redisUtil;
  17.  
  18. @Override
  19. public Map<String, Object> addFood(String name) {
  20. Map<String, Object> map = new HashMap<>();
  21. if (StringUtils.isBlank(name)) {
  22. map.put("data", "name不可为空");
  23. return map;
  24. }
  25. name = "food_" + name;
  26.  
  27. int k = 1;
  28. if (redisUtil.hasKey(name)) {
  29. k = (int) redisUtil.get(name) + 1;
  30. }
  31. if (redisUtil.set(name, k)) {
  32. map.put("data", "添加成功");
  33. } else {
  34. map.put("data", "添加失败");
  35. }
  36. return map;
  37. }
  38.  
  39. @Override
  40. public Map<String, Object> getAll() {
  41. Map<String, Object> map = new HashMap<>();
  42. Set<String> strings = redisUtil.keys("food_*");
  43. List<Object> list = new ArrayList<>();
  44. for (String s : strings) {
  45. list.add(s + "-" + redisUtil.get(s));
  46. }
  47. map.put("state", 111);
  48. map.put("data", list);
  49. return map;
  50. }
  51. }

注意了 ,调用redis不再是使用 jedis 了 ,在spring boot 是使用 redisTemplate 的 ,在工具的 RedisUtil底层是 使用redisTemplate调用redis指令  ,但 部分语法与jedis有点区别 ,但是不影响使用

(8)controller层

  1. package com.example.providerredis8003.controller;
  2.  
  3. import com.example.providerredis8003.service.EatService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. import java.util.Map;
  10.  
  11. @RestController
  12. public class FoodController {
  13.  
  14. @Autowired
  15. private EatService eatService;
  16.  
  17. /**
  18. * 添加食物,如果已经在redis存在,则数量加一,否则新建
  19. */
  20. @RequestMapping("/add")
  21. public Map<String,Object> addFood(String name){
  22. return eatService.addFood(name);
  23. }
  24.  
  25. /**
  26. * 获取所有食物
  27. */
  28. @RequestMapping("/get")
  29. public Map<String,Object> getAll(){
  30. return eatService.getAll();
  31. }
  32.  
  33. @RequestMapping(value = "/getname",method = RequestMethod.GET)
  34. public String getname(String name){
  35. System.out.println("接收名字="+name);
  36. return "我是端口8003,你三爷叫:"+name;
  37. }
  38.  
  39. }

(9)启动类 【不需要什么配置】

  1. package com.example.providerredis8003;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class ProviderRedis8003Application {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(ProviderRedis8003Application.class, args);
  11. }
  12.  
  13. }

4.测试

(1)访问端口 8003  ,http://localhost:8003/get  ,查询数据

redis无数据 ,但是说明已经连接成功了

(2)访问端口 8003  ,http://localhost:8003/add?name=apple  ,添加数据

添加成功

访问端口 8003  ,http://localhost:8003/add?name=egg  ,添加数据

添加成功

访问端口 8003  ,http://localhost:8003/get  ,查询数据

查询成功,有数据

(3)现在再加一个apple ,

再次 访问端口 8003  ,http://localhost:8003/add?name=apple  ,添加数据

添加成功

再次 访问端口 8003  ,http://localhost:8003/get  ,查询数据

可见再次添加后 apple的数字加 一 了

      成功,撒花!!!

简单的添加、修改 、查询 redis 操作成功完成 ,spring boot成功使用redis ,其他复杂的业务只需要修改redis工具即可完成对redis的操作

-------------------------------------------

参考博文原址:

https://www.jianshu.com/p/b9154316227e

https://blog.csdn.net/User_xiangpeng/article/details/53839702?utm_source=blogxgwz6

https://blog.csdn.net/qianfeng_dashuju/article/details/103859413?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

spring boot + redis --- 心得的更多相关文章

  1. 阿里架构师的这一份Spring boot使用心得:网友看到都收藏了

    阿里架构师的这一份Spring boot使用心得: 这一份PDF将从Spring Boot的出现开始讲起,到基本的环境搭建,进而对Spring的IOC及AOP进行详细讲解.以此作为理论基础,接着进行数 ...

  2. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  3. spring boot + redis 实现session共享

    这次带来的是spring boot + redis 实现session共享的教程. 在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring se ...

  4. Spring Boot 项目学习 (三) Spring Boot + Redis 搭建

    0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...

  5. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  6. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. spring boot redis分布式锁

    随着现在分布式架构越来越盛行,在很多场景下需要使用到分布式锁.分布式锁的实现有很多种,比如基于数据库. zookeeper 等,本文主要介绍使用 Redis 做分布式锁的方式,并封装成spring b ...

  8. 从.Net到Java学习第四篇——spring boot+redis

    从.Net到Java学习系列目录 “学习java已经十天,有时也怀念当初.net的经典,让这语言将你我相连,怀念你......”接上一篇,本篇使用到的框架redis.FastJSON. 环境准备 安装 ...

  9. Spring Boot Redis Cluster 实战干货

    添加配置信息 spring.redis: database: 0 # Redis数据库索引(默认为0) #host: 192.168.1.8 #port: 6379 password: 123456 ...

随机推荐

  1. 【Matlab】abs不支持复整数

    需要将uint8转换成double型数据才能计算 https://blog.csdn.net/lihe4151021/article/details/89372688 图像数据格式uint8与doub ...

  2. java 注解的几大作用及使用方法详解

    初学者可以这样理解注解:想像代码具有生命,注解就是对于代码中某些鲜活个体的贴上去的一张标签.简化来讲,注解如同一张标签. 在未开始学习任何注解具体语法而言,你可以把注解看成一张标签.这有助于你快速地理 ...

  3. python使用gitlab-api

    目录 一.简介 二.示例 讲解 配置文件方式存储token 其它返回 三.其它操作 一.简介 公司使用gitlab 来托管代码,日常代码merge request以及其他管理是交给测试,鉴于操作需经常 ...

  4. JSONP是个嘛玩意?解决跨域问题?

    浏览器同源策略 限制js向 其他域名发起请求,浏览器调试报错如下 JSONP 是一种解决方法 浏览器不会阻止带有src属性的标签发请求.所以可以常用的 <script src="xxx ...

  5. 网站高可用架构之BASE原理

    BASE理论是eBay架构师提出的. BASE定理来源:是CAP中一致性和可用性的权衡结果,它来自大规模互联网分布式系统的总结,是基于CAP定理逐步演化而来的. BASE定理的核心思想:即使无法做到强 ...

  6. Spring学习(一)idea中创建第一个Spring项目

    1.前言 Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container). Sprin ...

  7. JAVA将Object对象转byte数组

    /** * 将Object对象转byte数组 * @param obj byte数组的object对象 * @return */ public static byte[] toByteArray(Ob ...

  8. Centos(Linux)安装openoffice教程

    一.从官网下载openoffice软件 下载地址:http://www.openoffice.org/zh-cn/download/ 选择(RPM)类型进行下载,选择对应的版本,这里默认选择是最新的版 ...

  9. Linux宝塔面板部署运行jar包

    登录面板 安装插件 把jar包上传上去 设置jar包 填写项目启动的端口 然后点击确定 会自动启动 然后浏览器打开 ip:端口 即可

  10. PC chrome开启自带的dark mode

    地址 复制下面的地址到chrome地址栏打开,再设置为 Enable 就可以开启了. chrome://flags/#enable-force-dark