SpringBoot(三)缓存
1 默认缓存管理
Spring框架支持透明地向应用程序添加缓存对缓存进行管理,其管理缓存地核心是将缓存应用于操作数据地方法,从而减少操作数据地执行次数,同时不会对程序本身造成任何干扰。
Spring Boot继承了Spring框架的缓存管理功能,通过使用@EnableCaching注解开启基于注解的缓存支持,Spring Boot就可以启动缓存管理的自动化配置。
1.1 环境搭建
1.1.1 准备数据
创建一个lxsh_test的数据库,该数据库有两个表t_article和t_comment
1.1.2 创建项目,功能编写
(1) 在Dependencies依赖选择项中添加SQL模块中的JPA依赖、MySQL依赖和Web模块中的Web依赖
(2) 编写数据库表对应的实体类,并使用JPA相关注解配置映射关系
import javax.persistence.*;
@Entity(name = "t_comment")
public class Comment {
@Id//表映射对应的主键ID
@GenerateValue(strategy = GenerationType.IDENTITY)//设置主键策略
private Integer id;
private String content;
private String author;
@Column(name = "a_id")//指定映射的表字段名
private Integer aId;
//省略getXX()和setXX()方法
//省略toString()方法
}
(3) 编写数据库操作的Repository接口文件
public interface CommentRepository extends JpaRepositroy<Comment,Integer> {
//根据评论id修改评论作者author
@Transactional
@Modifying
@Query("update t_comment c set c.author=?1 where c.id=?2",nativeQuery = true)
public int updateComment(String author,Integer id);
}
(4) 编写Service层
@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
public Comment findCommentById(Integer id){
Optional<Comment> comment = commentRepository.findById(id);
if(comment.isPresent()){
Comment model = comment.get();
return model;
}
return null;
}
}
(5) 编写Controller层
@RestController
public class CommentController {
@Autowired
private CommentService commentService;
@GetMapping("/findCommentById")
public Comment findCommentById(Integer id){
Comment comment = commentService.findCommentById(id);
return comment;
}
}
(6) 编写配置文件
在项目全局配置文件application.yml中编写对应的数据库连接配置
spring:
#MySQL数据库连接配置
datasource:
url: jdbc:mysql://localhost:3306/lxsh_test?serverTimezone=UTC
username: root
password: root
jpa:
show-sql: true # 显示使用JPA进行数据查询的sql语句
http:
encoding:
force-response: true # 解决乱码
(7) 运行项目进行测试
1.2 默认缓存体验
在前面搭建的Web应用基础上,开启Spring Boot默认支持的缓存,体验Spring Boot默认缓存的使用效果
(1) 使用@EnableCaching注解开启基于注解的缓存支持
@EnableCaching //开启Spring Boot基于注解的缓存管理支持
@SpringBootApplication
public class TestApplication {
public static void main(String[] args){
SpringApplication.run(TestApplication.class,args);
}
}
(2) 使用@Cacheable注解对数据操作方法进行缓存管理。将@Cacheable注解标注在Service类的查询方法上,对查询结果进行缓存
//根据评论ID查询评论信息
//key:默认在只有一个参数的情况下,key值默认就是方法参数值;如果没有参数或者多个参数的情况,simpleKeyGenerate生成key
//value:缓存结果
@Cacheable(cacheNames= "comment")
public Comment findCommentById(Integer id){
Optional<Comment> comment = commentRepository.findById(id);
if(comment.isPresent()){
Comment model = comment.get();
return model;
}
return null;
}
上述代码中,在CommentService类中的findCommentById(Integer id)方法上添加了查询缓存注解@Cacheable,该注解的作用是将查询结果Comment存放在Spring Boot默认缓存中名为comment的名称空间(namespace)中,对应缓存唯一标识(即缓存数据对应的主键k)默认为方法参数id的值
(3) 访问结果分析
在诸多的缓存自动配置类中,SpringBoot默认装配的是SimpleCacheConfiguration,它使用的CacheManager是ConcurrentMapCacheManager,使用CurrentMap当底层数据结构,按照Cache的名字查询出Cache,每一个Cache中存在多个k-v键值对,缓存值
1.3 缓存注解介绍
上述代码中使用@EnableCaching、@Cacheable注解实现了Spring Boot默认的基于注解的缓存管理,除此之外,还有更多的缓存注解以及注解属性可以配置优化缓存管理
1.3.1 @EnableCaching注解
@EnableCaching是由Spring框架提供的,SpringBoot框架对该注解进行了继承,该注解需要配置在类上(通常是在项目启动类上),用于开启基于注解的缓存支持
1.3.2 @Cacheable注解
@Cacheable注解也是由Spring框架提供的,可以作用于类或方法(通常用在数据查询方法上),用于对方法结果进行缓存存储。注解的执行顺序是,先进行缓存查询,如果为空则进行方法查询,并将结果进行缓存;如果缓存中有数据,不进行方法查询,而是直接使用缓存数据
@Cacheable注解提供了多个属性,用于对缓存存储进行相关配置
属性名 | 说明 |
---|---|
value/cacheNames | 指定缓存空间的名称,必配属性。这两个属性二选一使用 |