依赖

  1. <dependencies>
  2. <!--web依赖-->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!--Redis 依赖-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-data-redis</artifactId>
  11. </dependency>
  12. <!--测试-->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-test</artifactId>
  16. <scope>test</scope>
  17. </dependency>
  18. <!-- lombok依赖 -->
  19. <dependency>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok</artifactId>
  22. <optional>true</optional>
  23. </dependency>
  24. </dependencies>

配置

  1. spring:
  2. redis:
  3. port: 6379
  4. host: 127.0.0.1
  5. password: redis
  6. database: 0

Redis工具类

  1. /**
  2. * @description:
  3. * @author: Jotal
  4. * @time: 2019/8/17 21:29
  5. */
  6. @Component("redisUtils")
  7. public class RedisUtil {
  8. @Resource
  9. private StringRedisTemplate stringRedisTemplate;
  10. /**
  11. * @Description: 获取
  12. * @Param: [key]
  13. * @Return: java.lang.String
  14. * @Author: Jotal
  15. * @Date: 2019/8/17 21:39
  16. */
  17. public String get(String key) {
  18. try {
  19. if (StringUtils.isEmpty(key)) {
  20. return null;
  21. }
  22. return stringRedisTemplate.opsForValue().get(key);
  23. } catch (Exception e) {
  24. System.out.println(String.format("redis缓存获取key的值异常!key:%s", key));
  25. e.printStackTrace();
  26. }
  27. return null;
  28. }
  29. /**
  30. * @Description: 设置键值对
  31. * @Param: [key, value]
  32. * @Return: java.lang.Boolean
  33. * @Author: Jotal
  34. * @Date: 2019/8/17 21:43
  35. */
  36. public Boolean set(String key,String value) {
  37. try {
  38. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
  39. return false;
  40. }
  41. stringRedisTemplate.opsForValue().set(key, value);
  42. return true;
  43. } catch (Exception e) {
  44. System.out.println(String.format("redis缓存设置键值对!key:%s,value:%s", key,value));
  45. e.printStackTrace();
  46. }
  47. return false;
  48. }
  49. /**
  50. * @Description: 删除键值对
  51. * @Param: [key]
  52. * @Return: java.lang.Boolean
  53. * @Author: Jotal
  54. * @Date: 2019/8/17 21:47
  55. */
  56. public Boolean del(String key) {
  57. try {
  58. if (StringUtils.isEmpty(key)) {
  59. return false;
  60. }
  61. return stringRedisTemplate.delete(key);
  62. } catch (Exception e) {
  63. System.out.println(String.format("redis删除键值对!key:%s", key));
  64. e.printStackTrace();
  65. }
  66. return false;
  67. }
  68. /**
  69. * @Description: 设置键值对和缓存时间,单位为秒
  70. * @Param: [key, value, time]
  71. * @Return: java.lang.Boolean
  72. * @Author: Jotal
  73. * @Date: 2019/8/17 21:49
  74. */
  75. public Boolean setEX(String key, String value, Long time) {
  76. try {
  77. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
  78. return false;
  79. }
  80. stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
  81. //设置缓存时间
  82. //stringRedisTemplate.expire(key,time, TimeUnit.SECONDS);
  83. return true;
  84. } catch (Exception e) {
  85. System.out.println("设置缓存异常");
  86. e.printStackTrace();
  87. }
  88. return false;
  89. }
  90. /**
  91. * @Description: 获取key的缓存时间
  92. * @Param: [key]
  93. * @Return: java.lang.Long
  94. * @Author: Jotal
  95. * @Date: 2019/8/17 21:55
  96. */
  97. public Long getExpireTime(String key) {
  98. try {
  99. if (StringUtils.isEmpty(key)) {
  100. return null;
  101. }
  102. return stringRedisTemplate.getExpire(key,TimeUnit.SECONDS);
  103. } catch (Exception e) {
  104. System.out.println("获取缓存异常");
  105. e.printStackTrace();
  106. }
  107. return null;
  108. }
  109. }

单元测试

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. @Slf4j
  4. public class Springboot10RedisApplicationTests {
  5. //@Resource是根据名字来自动装配 @Autowired是根据类型来自动装配
  6. @Resource
  7. private RedisUtil redisUtils;
  8. @Test
  9. public void setTest() {
  10. Boolean bl = redisUtils.set("jotal", "jotal1314");
  11. log.info("设置键值对"+bl);
  12. }
  13. @Test
  14. public void getTest() {
  15. String value = redisUtils.get("welcome");
  16. log.info("获取值:"+value);
  17. }
  18. @Test
  19. public void testDelete() {
  20. Boolean flag = redisUtils.del("jotal1");
  21. log.info("testDelete:"+flag);
  22. }
  23. @Test
  24. public void testSetEX() {
  25. Boolean flag = redisUtils.setEX("welcome","www",1000L);
  26. log.info("testSetEX:"+flag);
  27. }
  28. @Test
  29. public void testGetExpireTime() {
  30. Long time = redisUtils.getExpireTime("welcome");
  31. log.info("testSetEX:"+time);
  32. }
  33. }

观察Redis数据库中的键值对变化!

springboot笔记10——整合Redis的更多相关文章

  1. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

    SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...

  2. springboot学习笔记-3 整合redis&mongodb

    一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...

  3. SpringBoot: 10.整合mybatis(转)

    需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...

  4. 【快学springboot】11.整合redis实现session共享

    前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...

  5. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  6. SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制

    一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...

  7. 25、springboot与缓存整合Redis

    默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...

  8. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  9. springboot笔记08——整合swagger2

    Swagger是什么? Swagger是一个RESTFUL 接口的文档在线自动生成和功能测试的框架.利用swagger2的注解可以快速的在项目中构建Api接口文档,并且提供了测试API的功能. Spr ...

随机推荐

  1. NIOBuffer 缓冲区

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但是,Channel,Buffer 和 Select ...

  2. fastjson WriteClassName,Double类型不打3.3D

    方式一: public class SerializeConfigX extends SerializeConfig { public SerializeConfigX() { put(Double. ...

  3. python skimage图像处理(二)

    python skimage图像处理(二) This blog is from: https://www.jianshu.com/p/66e6261f0279  图像简单滤波 对图像进行滤波,可以有两 ...

  4. 一个android任务提醒程序

    需求: 运行建立多个提醒,每个提醒设置一个时间,到达指定时间后跳出提醒窗体 每个提醒有一个执行按钮,点击后保存执行记录,并且当天不再触发提醒窗体 提醒出现后如果任务还没执行,那么需要在30分钟后再次提 ...

  5. 如何防止Hangfire重复作业在连续执行30分钟后重新启动(How to prevent a Hangfire recurring job from restarting after 30 minutes of continuous execution)

    var options = new SqlServerStorageOptions { InvisibilityTimeout = TimeSpan .FromMinutes(30)//默认值}; G ...

  6. Anaconda(三)

    五.TensorFlow安装 这一天由于版本问题走了太多弯路.之前用的conda版本是最新的,自带Python3.7.5,装了之后倒是各种包都能装,用命令: pip install xxx conda ...

  7. Egret HTML5游戏开发指南

    Egret  HTML5游戏开发指南 下载地址:https://pan.baidu.com/s/1fuxllvmRhWXoWDwH4gxN9g 关注微信公众号获取提取码: 输入:egrt 获取提取码

  8. CentOS / RHEL 内核升级

    1. 查看当前内核版本 [root@192.168.118.11 ~]#cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [ro ...

  9. 部署TiDB集群

    架构图 节点规划 120.52.146.213 Control Machine 120.52.146.214 PD1_TiDB1 120.52.146.215 PD2_TiDB2 120.52.146 ...

  10. java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder

    添加依赖即可:compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6' ,注意是apache的包