一 、引入依赖(redis):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis的配置以及使用,这不做介绍,已经整合过reids,没有配置redis的请先整合好。
二、启动类开启注解
@EnableCaching
三、正常编码加入注解
service 层
// service层方法
@Cacheable(key = "#id")
List<StoreVo> geetList(String id);
// service实现层方法
@Override
public List<StoreVo> geetList(String id) {
System.out.println("查询数据库");
List<StoreVo> lists = new ArrayList<>();
StoreVo storeVo= new StoreVo ();
storeVo.setCode("Code0001");
lists.add(storeVo);
return lists;
}
直接调用服务查看日志:
测试缓存
查询数据库
2023-07-27 11:17:25.545 DEBUG 45612 --- [ XNIO-1 task-1] com.shop.security.filter.AuthFilter : AuthFilter:http://localhost:9085/testCache/pageStore
2023-07-27 11:17:25.546 DEBUG 45612 --- [ XNIO-1 task-1] com.shop.security.filter.AuthFilter : AuthFilter:/testCache/pageStore
测试缓存
缓存注解生效。
注意:缓存时间是在RedisCacheManager中配置的,在整合redis是就设置了缓存时间。