Spring Boot 之 springcache的使用
一、开启 springcache,启动类添加 @EnableCaching 注解
@SpringBootApplication
@EnableCaching
public class GatheringApplication { public static void main(String[] args) {
SpringApplication.run(GatheringApplication.class, args);
}
}
二、添加缓存,修改 findById 方法 通过 @Cacheable 添加缓存
/**
* 根据ID查询实体
* @param id
* @return
*/
@Cacheable(value = "gathering",key = "#id")
public Gathering findById(String id) {
System.out.println("查询次数:"+i++);
return gatheringDao.findById(id).get();
}
三、删除缓存,修改 update 和 deleteById 方法 通过 @CacheEvict 删除缓存
/**
* 修改
* @param gathering
*/
@CacheEvict(value = "gathering",key = "#gathering.id")
public void update(Gathering gathering) {
gatheringDao.save(gathering);
} /**
* 删除
* @param id
*/
@CacheEvict(value = "gathering",key = "#id")
public void deleteById(String id) {
gatheringDao.deleteById(id);
}
四、执行结果
1、点击了5次查询,只有一次进入了方法体并,访问了数据库。其余4次都是从缓存中取数据。 查询次数:1
Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=? 2、执行修改方法
Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
Hibernate: update tb_gathering set address=?, city=?, detail=?, endtime=?, enrolltime=?, where id=?
3、再次查询(点击5次),因为执行 修改方法时,也删除了缓存,所以本次查询可以进入方法体,并交互数据库
查询次数:2
Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
4、执行删除方法
Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
Hibernate: delete from tb_gathering where id=?
5、再次查询(点击5次),因为执行 删除方法时,也删除了缓存,所以本次查询可以进入方法体,并交互数据库,但未查询出结果
查询次数:3
Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
Spring Boot 之 springcache的使用的更多相关文章
- Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来
计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...
- 利用Spring Boot+zxing,生成二维码还能这么简单
在网站开发中,经常会遇到要生成二维码的情况,比如要使用微信支付.网页登录等,本文分享一个Spring Boot生成二维码的例子,这里用到了google的zxing工具类. 本文目录 一.二维码简介二. ...
- 玩转spring boot——快速开始
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 玩转spring boot——开篇
很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- 玩转spring boot——结合JPA入门
参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...
- 玩转spring boot——结合JPA事务
接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
随机推荐
- 50. Pow(x, n) (JAVA)
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- mkswap - 建立一个linux交换区
总览 mkswap [-c] [-vN] [-f] device [size] 描述 mkswap 在一个设备上或者在一个文件里创建一个linux交换区. (该交换区创建后,必须使用 swapon 命 ...
- SQL SERVER将多行数据合并成一行(转)
1)比如表中有三列数据: 2)执行如下查询: 1 SELECT [USER_NAME], [USER_ACCOUNT] 2 , [ROLE_NAME] = stuff(( 3 SELECT ',' + ...
- LoadPicture函数用法示例
VB语言中LoadPicture函数用法示例: 本例使用 LoadPicture 函数将图片加载到窗体的 PictureBox 控件并从控件上清除掉该图片. 要试用此例,将 PictureBox 控件 ...
- Saving James Bond - Easy Version
题目来源: 浙江大学在慕课网上开设的<数据结构>课,陈越老师.何钦铭老师主讲,课后作业的一道题. 题目描述: 题目思路: 这道题目本质上讲就是列出图的连通集,但是这个连通集的起点是有约束的 ...
- java集合类图详解
- thymeleaf常用模板方法收集
判断是不是为空1.th:if="${xxx} != null" th:if="${xxx != null}" 是不是为空字符串 1.th:if="${ ...
- Django【第1篇】:Django之MTV模型
Django框架第一篇基础 一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model ...
- SpringCloud学习系列-Eureka自我保护模式(5)
什么是自我保护模式? 默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒).但是当网络分区故障发生时,微服务与Eur ...
- iOS 推送角标解决方案
在App启动时:didFinishLaunchingWithOptions 方法中:application.applicationIconBadgeNumber = ; //角标清零 在读消息时: a ...