springboot-cache缓存的简单应用
一:引入pom依赖
<!-- 集成缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 引入ehcache支持 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
二:在启动类中加入@EnableCaching注解

三:应用
cacheNames/value:用来指定缓存组件的名字
key:缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写)
keyGenerator:key 的生成器。 key 和 keyGenerator 二选一使用
cacheManager:可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。
condition:可以用来指定符合条件的情况下才缓存
unless:否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过#result获取方法结果)
sync:是否使用异步模式。
condition
符合条件的情况下才缓存。方法返回的数据要不要缓存,可以做一个动态判断。(偷个图)
例如这个页面的图片需要缓存

1.在加载的时候可以这样写:
@Cacheable(value = "files" ,key = "'frontAll'")//集成缓存key是自己定义的

更新和新增的时候:
@CachePut(value = "files", key = "'frontAll'")

@Cacheable(cacheNames = "files",key = "'frontAll'")
public Student insert(Student student) {
this.studentMapper.insert(student);
return student;
}
删除的时候:
@CacheEvict(value = "files", key = "'frontAll'")


浙公网安备 33010602011771号