Spring Boot学习笔记23——缓存管理

本文介绍了Spring Boot的缓存管理,包括默认缓存体验、缓存注解及Redis缓存的集成。通过注解开启缓存支持,减少了数据库访问次数。详细探讨了Spring Boot如何与Redis结合,实现基于注解和API的缓存操作,以及自定义RedisTemplate和RedisCacheManager以优化序列化机制。
摘要由CSDN通过智能技术生成

缓存是分布式系统的重要组件,主要解决数据库数据的高并发访问问题,对于用户访问量大的网站,缓存对于提高服务器访问性能,减少数据库压力和提高用户体验十分重要。spring boot对缓存提供了很好的支持,下面我们将对spring boot的缓存进行介绍和对spring boot与redis缓存中间件进心整合

1. Spring Boot默认缓存管理

spring框架支持透明的向应用程序添加缓存并对缓存进行管理,其管理缓存的核心是将缓存应用于操作数据据的方法中,从而减少操作数据的次数,同时不对程序本身造成干扰。spring boot继承了spring的缓存管理功能,通过@EnableCaching注解开启基于注解的缓存支持,springboot可以启动缓存管理的自动化配置。下面我们对springboot支持的默认缓存管理进行讨论

1.1 继承环境搭建
  1. 准备数据
    直接使用前面用到的springbootdata数据库,里面有两个表格t_article和t_comment,然后我们预先插入几条测试数据
  2. 创建项目
    图片编写实体类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);
    }
}
  1. 编写配置文件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
  1. 项目测试
    启动项目并访问http://localhost:8080/get/1,会返回一条数据,同时可以发现控制台也会输出一条sql语句,如果多刷新几次页面,控制台的sql语句也会相应增加
    图片图片

之所以会出现这种情况,是因为没有开启缓存,这样数据表中的数据虽没有变化,但每执行一次查询操作(本质是执行sql语句),都会访问一次数据库,随着时间和访问量的增加,数据规模会越来越大,这样会影响用户体验,为此我们要使用缓存来解决问题

1.2 spring boot默认缓存体验
  1. 在项目启动类上使用@EnableCaching注解开启缓存支持
@EnableCaching   //开启缓存支持
@SpringBootApplication
public class CacheApplication {
   
    public static void main(String[] args) {
   
        SpringApplication.run(CacheApplication.class, args);
    }
}

2.在Service类的查询方法上使用@Cacheable注解对数据操作方法进行缓存管理

@Cacheable(cacheNames = "comment")
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值