一、pom.xml引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、修改application.yml ,在spring节点下添加配置

  redis:
host: 127.0.0.1

三、修改Service 引入RedisTemplate

  1、在 findById 方法中 添加Redis缓存

@Autowired
private RedisTemplate redisTemplate;
int i=1;
  /**
* 根据ID查询实体
* @param id
* @return
*/
public Article findById(String id) {
System.out.println("查询次数:"+i++);
//先从缓存中查询当前对象
Article article=(Article)redisTemplate.opsForValue().get("article_"+id);
//如果没有取到
if(article==null){
//从数据库中查询
article=articleDao.findById(id).get();
//存入到缓存中
redisTemplate.opsForValue().set("article_"+id,article);
}
return article;
}

  执行结果

查询次数:1
Hibernate: select article0_.id as id1_0_0_, article0_.channelid as channeli2_0_0_ from tb_article article0_ where article0_.id=?
查询次数:2
查询次数:3
查询次数:4

  2、在 update 和 deleteById 方法中 删除Redis缓存

    /**
* 修改
* @param article
*/
public void update(Article article) {
redisTemplate.delete("article_"+article.getId());
articleDao.save(article);
} /**
* 删除
* @param id
*/
public void deleteById(String id) {
redisTemplate.delete("article_"+id);
articleDao.deleteById(id);
}

  操作流程:先修改 update 再查询 findById ,

       再修改 deleteById ,在查询 findById ,

  执行结果:

查询次数:5
Hibernate: update tb_article set channelid=?, columnid=?, comment=?, content=?, createtime=?, image=?, ispublic=?, istop=?, state=?, thumbup=?, title=?, type=?, updatetime=?, url=?, userid=?, visits=? where id=?
查询次数:6
Hibernate: select article0_.id as id1_0_0_, article0_.channelid as channeli2_0_0_ from tb_article article0_ where article0_.id=?
查询次数:6
查询次数:7
查询次数:8
查询次数:9
查询次数:10
Hibernate: delete from tb_article where id=?
查询次数:11
Hibernate: select article0_.id as id1_0_0_, article0_.channelid as channeli2_0_0_ from tb_article article0_ where article0_.id=?

  3、设置 Redis 过期时间 10秒

  /**
* 根据ID查询实体
* @param id
* @return
*/
public Article findById(String id) {
System.out.println("查询次数:"+i++);
//先从缓存中查询当前对象
Article article=(Article)redisTemplate.opsForValue().get("article_"+id);
//如果没有取到
if(article==null){
//从数据库中查询
article=articleDao.findById(id).get();
//存入到缓存中
redisTemplate.opsForValue().set("article_"+id,article,10, TimeUnit.SECONDS);
}
return article;
}

  执行结果:

查询次数:1
Hibernate: select article0_.id as id1_0_0_, article0_.channelid as channeli2_0_0_ from tb_article article0_ where article0_.id=?
查询次数:2
查询次数:3
查询次数:4
查询次数:5
查询次数:6
Hibernate: select article0_.id as id1_0_0_, article0_.channelid as channeli2_0_0_ from tb_article article0_ where article0_.id=?
查询次数:7
查询次数:8
查询次数:9

前5次查询只有第一次访问了数据库,想个10秒后,

再次查询4次,只有第6次访问了数据库,后3次也没有访问数据库。

四、SpringDataRedis常用方法

redisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
redisTemplate.opsForValue().get("test")//根据key获取缓存中的val
redisTemplate.boundValueOps("test").increment(-1);//val做-1操作
redisTemplate.boundValueOps("test").increment(1);//val +1
redisTemplate.getExpire("test")//根据key获取过期时间
redisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位
redisTemplate.delete("test");//根据key删除缓存
redisTemplate.hasKey("546545");//检查key是否存在,返回boolean值
redisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间
redisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
redisTemplate.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据
redisTemplate.opsForSet().members("red_123");//根据key获取set集合

Spring Boot 之 Redis的更多相关文章

  1. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  2. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

  3. 15套java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...

  4. spring boot集成redis实现session共享

    1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...

  5. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  6. spring boot 结合Redis 实现工具类

    自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...

  7. (转)spring boot整合redis

    一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...

  8. Spring Boot 结合 Redis 缓存

    Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...

  9. SpringBoot(三) :Spring boot 中 Redis 的使用

    前言: 这一篇讲的是Spring Boot中Redis的运用,之前没有在项目中用过Redis,所以没有太大的感觉,以后可能需要回头再来仔细看看. 原文出处: 纯洁的微笑 SpringBoot对常用的数 ...

  10. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

随机推荐

  1. Linux 设置定时清除buff/cache的脚本

    Linux 设置定时清除buff/cache的脚本 查看内存缓存状态 [root@heyong ~]# free -m total used free shared buff/cache availa ...

  2. 动态SQL的注意

    MyBatis的动态SQL元素. 元素 说明 <if> 判断语句,用于单条件分支判断 <choose>(<when>.<otherwise>) 相当于j ...

  3. python基础练习题1

    深深感知python基础是有多么重要,Ljh说一定要多练题,so,我现在开始要每天打卡练习python.加油! 01:求‘1-100’的偶数和 #第一种解法: sum=0 num=0 while nu ...

  4. <转>ThinkPHP的开发常用系统配置项

    /* 项目设定 */ ’APP_DEBUG’ => false, // 是否开启调试模式 ’APP_DOMAIN_DEPLOY’ => false, // 是否使用独立域名部署项目 ’AP ...

  5. 洛谷 P4665 [BalticOI 2015]Network

    洛谷 P4665 [BalticOI 2015]Network 你有一棵 $ n $ 个节点的树,你可以在树上加一些边,使这棵树变成一张无重边.自环的图,且删掉任意一条边它仍然联通.求最少要加多少条边 ...

  6. 36.React基础介绍——2019年12月24日

    2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...

  7. 与Swing的初见

    ---------------------------参考菜鸟教程的swing课程学习-------------------- Swing 是一个为Java设计的GUI工具包. Swing是JAVA基 ...

  8. shell练习--PAT题目1002:写出这个数(失败案例)

    读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内输出 n 的 ...

  9. HTML中表格table标签的实例

    一.表格有边框,第一行居中对齐 二.表格没有边框 三.表格有水平标题 四.表格有垂直标题 五.合并行单元格 colspan合并单元格 六.表格有单元格边距(内边距) 七.表格没有单元格间距 八.表格有 ...

  10. jAVA基础 提高文件复制性能之多线程复制文件

    利用IO流中的随机访问文件 RandomAccessFile 和文件通道 FileChanne 复制文件可大大提高文件的读写效率,在此基础上利用多线程复制文件使其性能更优.因线程的个数可根据文件的大小 ...