文章目录
前言
我们知道内存的读取速度远大于硬盘的读取速度。当需要重复地获取相同数据时,一次一次地请求数据库或者远程服务,导致在数据库查询或远程方法调用上消耗大量的时间,最终导致程序性能降低,这就是数据缓存要解决的问题。
Spring Cache 是一个非常优秀的缓存组件。自Spring 3.1起,提供了类似于@Transactional注解事务的注解Cache支持,且提供了Cache抽象,方便切换各种底层Cache(如:redis)
使用Spring Cache的好处
1,提供基本的Cache抽象,方便切换各种底层Cache;
2,通过注解Cache可以实现类似于事务一样,缓存逻辑透明的应用到我们的业务代码上,且只需要更少的代码就可以完成;
3,提供事务回滚时也自动回滚缓存;
4,支持比较复杂的缓存逻辑;
一、基本用法
SpringCache缓存功能的实现是依靠下面的这几个注解完成的。
-
@EnableCaching:开启缓存功能
-
@Cacheable:获取缓存
-
@CachePut:更新缓存
-
@CacheEvict:删除缓存
-
@Caching:组合定义多种缓存功能
-
@CacheConfig:定义公共设置,位于类之上
先从spring cache中获取分类数据的功能,如果获取不到再从redis中获取,再获取不到才从mysql中获取。
二、使用介绍
2.1.@EnableCaching
@EnableCaching注解是缓存的开关,如果要使用缓存功能,就必要打开这个开关,这个注解可以定义在Configuration类或者springboot的启动类上面。
2.2.@Cacheable
@Cacheable可以标记在一个方法上,也可以标记在一个类上。
当标记在一个方法上时表示该方法是支持缓存的;当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,在方法执行前,Spring先检查缓存中是否存在该方法返回的数据,如果存在,则直接返回缓存数据;如果不存在,则调用方法并将方法返回值写入缓存。
@Cacheable注解经常使用value、key、condition等属性
- value:缓存的名称,指定一个或多个缓存名称。(该属性与cacheNames属性意义相同)
@Cacheable(value="mycache")或者@Cacheable(value={"cache1","cache2"})
- key:缓存的key,可以为空。如果指定。需要按照SpEL编写;如果不指定,则默认按照方法的所有参数进行组合。如
@Cacheable(value="testcache",key="#student.id")
- condition:缓存的条件,可以为空,如果指定,需要按照SpEL编写,返回true或者false,只有为true才进行缓存。如
@Cacheable(value="testcache",condition="#student.id>2")
2.3.@CacheEvict
@CacheEvict是用来标注在需要清除 缓存元素的方法或类上的。当标记在一个类上时,表示其中所有方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、conditon、allEntries和beforeInvocation。
其中,value、key和condition的语义与@Cacheable对应的属性类似。
- allEntries:是否清空所有缓存内容,默认为false,如果指定为true,则方法调用后将立即清空所有缓存。如
@CacheEvict(value="testcache",allEntries=true)
- beforeInvocation:是否在方法执行前就清空,默认为false,如果指定为true,则在方法还没有执行时就清空缓存。默认情况下,如果方法执行抛出异常,则不会清空缓存。
2.4. @Cacheput
使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库。一般用在新增方法上。
2.5.@Caching
该注解可以在一个方法或类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。示例代码如下:
@Caching(
cacheable=@Cacheable("cache1"),
put = {@CachePut(/*value = "emp",*/key = "#result.id")}
evict{@CacheEvict("cache2"),@CacheEvict(value="cache3",allEntries=true)}
)
2.6.@CacheConfig
所有的Cache注解都需要提供Cache名称,如果每个Service方法上都包含相同的Cache名称,可能写起来重复。此时可以使用@CacheConfig注解作用在类上,设置当前缓存的一些公共配置。
@CacheConfig(cacheNames = "emp")
@Service
public class EmployeeService
三.项目中如何使用
3.1 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version> 2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
3.2配置
spring:
cache:
type: redis
redis:
time-to-live: 3600000
# 缓存前缀,如果指定了前缀,就用指定的,如果没有,就使用缓存的名字作为前缀
key-prefix: CACHE_
use-key-prefix: true
# 是否缓存null值,防止缓存穿透
cache-null-values: true
3.2添加EnableCaching
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.3业务方法添加@Cacheable
@Cacheable(cacheNames = {"task"})
public Student getTask(String taskId) {
log.info("student mock query from DB......");
Student student= new Student ();
student.setTaskId(TaskId);
student.setApplicantId("ApplicantId");
student.setDescription("test1");
return student ;
}
3.4控制层发送请求:
@GetMapping("/test_cache")
public R<Student > testCache() {
Student student = this.testBuzz.getTask("task123");
return R.Success(student );
}
。