springboot06-cache

本文介绍了SpringBoot中缓存的使用,包括@Cacheable、@CachePut、@CacheEvict和@Caching等注解的详细解释,以及如何整合Redis进行缓存操作。同时,提到了通过导入依赖和配置文件实现SpringBoot与Redis的自动化配置。
摘要由CSDN通过智能技术生成

SpringBoot缓存

作用

将运行结果缓存,以后再要相同的数据,直接从缓存中获取,不用再调用方法;
springboot默认使用的是ConCurrentMapManeger

重要知识点

在这里插入图片描述

注解讲解

1.@Cacheable:添加缓存组件,一般在方法执行前执行,先查询cache组件map,有则直接用没有则执行方法

    @Cacheable(cacheNames = {
   "dept"},keyGenerator = "myKeyGenerator")
    public Department getDeptById(Integer id){
   
        Department department = departmentMapper.getDeptById(id);
        return department;
    }

2.@CachePut:添加缓存,先执行方法再更新缓存,用于更新数据库

    @CachePut(value = {
   "dept"})
    public Department updateDept(Department department){
   
        departmentMapper.updateDept(department);
        System.out.println("service方法执行");
        return department;
    }

3.@CacheEvict:删除缓存

    @CacheEvict(cacheNames = "dept",key = "#id")
    public void delDept(Integer id){
   
        departmentMapper.deleteDeptById(id)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值