缓存是分布式系统的重要组件,主要解决数据库数据的高并发访问问题,对于用户访问量大的网站,缓存对于提高服务器访问性能,减少数据库压力和提高用户体验十分重要。spring boot对缓存提供了很好的支持,下面我们将对spring boot的缓存进行介绍和对spring boot与redis缓存中间件进心整合
1. Spring Boot默认缓存管理
spring框架支持透明的向应用程序添加缓存并对缓存进行管理,其管理缓存的核心是将缓存应用于操作数据据的方法中,从而减少操作数据的次数,同时不对程序本身造成干扰。spring boot继承了spring的缓存管理功能,通过
@EnableCaching
注解开启基于注解的缓存支持,springboot可以启动缓存管理的自动化配置。下面我们对springboot支持的默认缓存管理进行讨论
1.1 继承环境搭建
- 准备数据
直接使用前面用到的springbootdata数据库,里面有两个表格t_article和t_comment,然后我们预先插入几条测试数据 - 创建项目
编写实体类domain.Comment,对应t_comment,并使用JPA注解配置映射关系
@Entity(name="t_comment")//设置ORM实体类,并指定映射表名
public class Comment {
@Id//表明映射对应的主键id
@GeneratedValue(strategy = GenerationType.IDENTITY)//设置主键自增策略
private Integer id;
private String content;
private String author;
@Column(name = "a_id")//指定映射的表字段名
private Integer aId;
//省略get-set和toString方法
}
编写数据库操作的repository.Repository接口文件,用于操作Comment实体
public interface Repository extends JpaRepository<Comment,Integer> {
@Transactional
@Modifying
@Query("UPDATE t_comment c SET c.author= ?1 WHERE c.id = ?2")
public int updateComment(String author,Integer id);//根据评论id修改评论作者author
}
编写业务操作类service.CommentService,包括查询,更新和删除功能
@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
public Comment findById(int comment_id){
Optional<Comment> optional = commentRepository.findById(comment_id);
if(optional.isPresent()){
return optional.get();
}
return null;
}
public Comment updateComment(Comment comment){
commentRepository.updateComment(comment.getAuthor(),comment.getaId());
return comment;
}
public void deleteComment(int comment_id){
commentRepository.deleteById(comment_id);
}
}
编写web访问层controller.CommentController
@RestController
public class CommentController {
@Autowired
private CommentService commentService;
@GetMapping("/get/{id}")
public Comment findById(@PathVariable("id") int comment_id){
Comment comment = commentService.findById(comment_id);
return comment;
}
@GetMapping("update/{id}/{author}")
public Comment updateComment(@PathVariable("id")int comment_id,
@PathVariable("author")String author){
Comment comment = commentService.findById(comment_id);
comment.setAuthor(author);
Comment updateComment = commentService.updateComment(comment);
return updateComment;
}
@GetMapping("/delete/{id}")
public void deleteComment(@PathVariable("id")int comment_id){
commentService.deleteComment(comment_id);
}
}
- 编写配置文件application.properties
# mysql数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=
spring.datasource.password=
# 显示使用JPA进行数据库查询的sql语句
spring.jpa.show-sql=true
- 项目测试
启动项目并访问http://localhost:8080/get/1,会返回一条数据,同时可以发现控制台也会输出一条sql语句,如果多刷新几次页面,控制台的sql语句也会相应增加
之所以会出现这种情况,是因为没有开启缓存,这样数据表中的数据虽没有变化,但每执行一次查询操作(本质是执行sql语句),都会访问一次数据库,随着时间和访问量的增加,数据规模会越来越大,这样会影响用户体验,为此我们要使用缓存来解决问题
1.2 spring boot默认缓存体验
- 在项目启动类上使用
@EnableCaching
注解开启缓存支持
@EnableCaching //开启缓存支持
@SpringBootApplication
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
2.在Service类的查询方法上使用@Cacheable
注解对数据操作方法进行缓存管理
@Cacheable(cacheNames = "comment")