第一步:添加依赖
<!--spring内置的缓存技术-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
第二步:在启动类上添加@EnableCaching
@SpringBootApplication
@MapperScan("com.example.demousermanagement.dao")
//将缓存开关打开
@EnableCaching
public class DemoUserManagementApplication {
public static void main(String[] args) {
SpringApplication.run(DemoUserManagementApplication.class, args);
}
}
第三步:在service实现方法上添加@Cacheable(value = "cacheInfoEle", key = "#id")
@Override
//创建一个缓冲区,缓冲区的名字叫cacheInfoEle(其实是个map),里面存放以#id为key的键值对
@Cacheable(value = "cacheInfoEle", key = "#id")
public Info queryById(String id) {
return infoDao.queryById(id);
}
spring提供的内置缓存功能
于 2023-12-13 21:12:39 首次发布
本文介绍了如何在SpringBoot项目中使用内置缓存技术,包括添加spring-boot-starter-cache依赖,配置启动类开启缓存,以及在Service方法上使用@Cacheable注解进行数据缓存。
摘要由CSDN通过智能技术生成