Spring Boot中Cache缓存的使用
项目描述
使用Springboot Cache可以有效的降低数据库压力,提升整个系统的相应效率和并发量。
一般的使用思路是:如果缓存中存在数据就从缓存中取数据,如果缓存中不存在数据就从数据库中读取。
实现原理: 利用了Spring AOP的动态代理技术,在项目启动的时候动态生成代理类,实现对应的逻辑。
运行环境
jdk8+mysql+IntelliJ IDEA+maven
项目技术(必填)
Springboot+mybatis+redis
数据库文件
是否原创
是
项目结构图
运行截图
Spring Boot使用Cache具体实现:
第一步:引入spring-boot-starter-cache包
第二步:在Spring Boot主类中增加@EnableCaching注解开启缓存功能,这一步很关键,否则缓存不起作用。
第三步:使用@Cacheable注解用来声明方法是可缓存的,将结果存储到缓存中后续调用同一个方法不需要执行实际的方法,直接从缓存中取值。@Cacheable 可以标记的方法上,也可以标记在一个类上,@Cacheable标记的方法上表示该方法支持缓存,标记在一个类上表示该类的所有方法都支持缓存。
helloCache可以理解成缓存中的key值,同一个参数第一次访问时走的是方法,往后走的都是缓存。可以打个断点Debug模式测试一下 测试:http://localhost:8080/hello?name=王二,第一次断点走到了
控制台打印出
然后clear一下控制台,刷新一下请求发现没有走断点,很快就得到结果,没有走断点,控制台也没有输出。
重新改变一下参数,返现又重新走了方法
@Cacheable 支持如下几个参数。
- value:缓存的名称。
- key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写;如果不指定,则缺省按照方法的所有参数进行组合。
- condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持 SpEL。
启动后在浏览器中输入网址 http://localhost:8080/condition?name=abc,第一次输出栏输出:没有走缓存!再次执行无输出,表明已经走缓存。在浏览器中输入网址http://localhost:8080/condition?name=abcde,浏览器执行多次仍然一直输出:没有走缓存!说明条件 condition 生效。
整合mybatis从数据库中取数据然后缓存
运行结果,断点试试,多次刷新看看走没走方法,并用下边的@CacheEvict所注解的方法清除缓存再试试
@CachePut
标注的方法在执行前,不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
@CachePut 配置方法
- value 缓存的名称。
- key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。
- condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存。
可以看出 @CachePut 的参数和使用方法基本和 @Cacheable 一致。
@CachePut 也可以标注在类上和方法上。
@CacheEvict
是用来标注在需要清除缓存元素的方法或类上的,当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict 可以指定的属性有 value、key、condition、allEntries 和 beforeInvocation,其中 value、key 和 condition 的语义与 @Cacheable 对应的属性类似。
即 value 表示清除操作是发生在哪些 Cache 上的(对应 Cache 的名称);key 表示需要清除的是哪个 key
allEntries 属性
allEntries 是 boolean 类型,表示是否需要清除缓存中的所有元素,默认为 false,表示不需要。当指定了 allEntries 为 true 时,Spring Cache 将忽略指定的 key,有的时候我们需要 Cache 一下清除所有的元素,这比一个一个清除元素更有效率。
beforeInvocation 属性
清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用 beforeInvocation 可以改变触发清除操作的时间,当我们指定该属性值为 true 时,Spring 会在调用该方法之前清除缓存中的指定元素。
猜你喜欢
- Spring Boot学习(九)之Spring Boot整合MyBatis 及注解配置 源码
- SpringBoot集成HIVE小例子,采用Druid管理连接池
- springboot整合jedis项目实例
- spring boot+mybatis+mysql项目搭建入门实例
- spring boot+mybatis+ehcache(缓存)整合的简单实例
- springBoot+shardingJdbc+mybaits+mapper3框架整合demo实例
- spring boot集成ehcache极简入门实例
- Spring Boot学习(四)之web开发渲染页面 -- Velocity
- Spring Boot学习(四)之web开发渲染页面 -- Freemarker博客源码分享
- spring boot1.3.1+mybatis+velocity学习整合demo分享
- SpringBoot+mybatis+Druid+Maven实现restful风格项目实例
- Spring boot + Freemarker基础语法学习demo
- /
- /springboot-cache
- /springboot-cache/.gitignore
- /springboot-cache/.idea
- /springboot-cache/.idea/artifacts
- /springboot-cache/.idea/artifacts/springboot_cache_war.xml
- /springboot-cache/.idea/artifacts/springboot_cache_war_exploded.xml
- /springboot-cache/.idea/compiler.xml
- /springboot-cache/.idea/encodings.xml
- /springboot-cache/.idea/inspectionProfiles
- /springboot-cache/.idea/inspectionProfiles/Project_Default.xml
- /springboot-cache/.idea/artifacts
- /springboot-cache/src
- /springboot-cache/src/main
- /springboot-cache/src/main/java
- /springboot-cache/src/main/java/com
- /springboot-cache/src/main/java/com/fei
- /springboot-cache/src/main/java/com/fei/Controller
/springboot-cache/src/main/java/com/fei/ServletInitializer.java
/springboot-cache/src/main/java/com/fei/SpringbootCacheApplication.java
- /springboot-cache/src/main/java/com/fei/entity
- /springboot-cache/src/main/java/com/fei/mapper
- /springboot-cache/src/main/java/com/fei
- /springboot-cache/src/main/java/com
- /springboot-cache/src/main/java
- /springboot-cache/src/main
- /springboot-cache

- 证 Spring Boot创建自定义Banner.txt实例
- 证 Spring Boot配置@Profile注解加载不同环境的配置文件实例
- 证 Spring Boot Actuator 2.3.4.RELEASE新版本实现自定义端点信息的配置实例
- 原证 spring AOP 过滤器 拦截器 执行顺序示例
- 证 spring boot使用不指定Maven parent pom来创建可执行的spring boot项目
- 证 Spring Boot整合thymeleaf做为显示层的hello world实例
- 证 Spring Boot整合SpringFox Swagger2实现REST API增删改查项目实例
- 证 Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例
- 证 Spring Boot整合Ehcache的简单入门实例
- 原证 springboot+sqlite3+iceEditor开发网页版记事本
- 原证精 SpringBoot+Maven+Echarts实现实时展示CPU内存硬盘性能
- 原证 Swagger的简单案例,适合初级者学习使用
