详解如何使用SpringBoot的缓存@Cacheable

 更新时间:2023年06月27日 11:28:40   作者:小石读史  
这篇文章主要为大家介绍了如何使用SpringBoot的缓存@Cacheable详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

缓存介绍

Spring 从 3.1 开始就引入了对 Cache 的支持。定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术。并支持使用 JCache(JSR-107)注解简化我们的开发。

其使用方法和原理都类似于 Spring 对事务管理的支持。Spring Cache 是作用在方法上的,其核心思想是,当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存在缓存中。

Cache 和 CacheManager 接口说明

Cache 接口包含缓存的各种操作集合,你操作缓存就是通过这个接口来操作的。

Cache 接口下 Spring 提供了各种 xxxCache 的实现,比如:RedisCache、EhCache、ConcurrentMapCache

CacheManager 定义了创建、配置、获取、管理和控制多个唯一命名的 Cache。这些 Cache 存在于 CacheManager 的上下文中。

小结:

每次调用需要缓存功能的方法时,Spring 会检查指定参数的指定目标方法是否已经被调用过,如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。

二、@Cacheable 注解使用详细介绍

@Cacheable 这个注解,用它就是为了使用缓存的。所以我们可以先说一下缓存的使用步骤:

缓存使用步骤

1、开启基于注解的缓存,使用 @EnableCaching 标识在 SpringBoot 的主启动类上。

2、标注缓存注解即可

① 第一步:开启基于注解的缓存,使用 @EnableCaching 标注在 springboot 主启动类上

@EnableSwagger2
@EnableScheduling
@EnableFeignClients(basePackages = {"src.main.biz.smallProject.client","com.codingapi.tx"})
@EnableJpaRepositories(basePackages = {"src.main.biz.smallProject.web.*.dao","src.main.newgrand.framework.common.dao" })
@EntityScan(basePackages = { "src.main.biz.smallProject.web.*.entity","src.main.newgrand.framework.common.domain"})
@EnableCaching
public class StartApp {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(StartApp.class);
        new SpringUtil().setApplicationContext(context);
    }
}

② 第二步:标注缓存注解

package src.main.biz.smallProject.web.cost.service.impl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import src.main.biz.smallProject.utils.CollectionUtils;
import src.main.biz.smallProject.web.construct.form.IdsForm;
import src.main.biz.smallProject.web.cost.dao.BusinessScopeJPA;
import src.main.biz.smallProject.web.cost.entity.BusinessScopeEntity;
import src.main.biz.smallProject.web.cost.entity.QBusinessScopeEntity;
import src.main.biz.smallProject.web.cost.form.BusinessScopeForm;
import src.main.biz.smallProject.web.cost.form.BusinessScopeQueryForm;
import src.main.biz.smallProject.web.cost.service.BusinessScopeService;
import src.main.biz.smallProject.web.cost.vo.BusinessScopeVO;
import src.main.newgrand.framework.common.constants.PageData;
import src.main.newgrand.framework.common.exception.BusinessException;
import src.main.newgrand.framework.common.service.impl.BaseServiceImpl;
import src.main.newgrand.framework.common.utils.ResultRes;
import java.time.LocalDateTime;
import java.util.List;
import static src.main.biz.smallProject.redis.CacheConstants.BUSINESS_SCOPE_CACHE;
/**
 * @Description
 * @Author    yql
 * @Date 2022-05-10 10:44:29 */
@Service
public class BusinessScopeServiceImpl extends BaseServiceImpl<BusinessScopeJPA, BusinessScopeEntity, BusinessScopeVO> implements BusinessScopeService {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    QBusinessScopeEntity qBusinessScopeEntity = QBusinessScopeEntity.businessScopeEntity;
    @Autowired
    private BusinessScopeService businessScopeService;
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    @CacheEvict(cacheNames = {BUSINESS_SCOPE_CACHE}, allEntries = true)
    public ResultRes add(BusinessScopeForm form) {
        BusinessScopeEntity businessScopeEntity = new BusinessScopeEntity();
        BeanUtils.copyProperties(form,businessScopeEntity);
        businessScopeService.save(businessScopeEntity);
        return ResultRes.success();
    }
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    @CacheEvict(cacheNames = {BUSINESS_SCOPE_CACHE}, allEntries = true)
    public ResultRes update(BusinessScopeForm form) {
        BusinessScopeEntity businessScopeEntity = findById(form.getPhid());
        if(businessScopeEntity == null){
            throw new BusinessException("数据不存在,请检查!");
        }
        BeanUtils.copyProperties(form,businessScopeEntity);
        businessScopeService.updateSelectiveById(businessScopeEntity);
        return ResultRes.success();
    }
    @Override
    @Cacheable(cacheNames = BUSINESS_SCOPE_CACHE, key = "{ " + "#root.methodName, #form.status}", unless = "#result == null")
    public ResultRes<PageData> list(BusinessScopeQueryForm form) {
        long currPage = form.getCurrent();
        long size = form.getSize();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qBusinessScopeEntity.phDelflag.eq(0L));
        if(form.getStatus() != null){
            booleanBuilder.and(qBusinessScopeEntity.status.eq(form.getStatus()));
        }
        QueryResults<BusinessScopeVO> results = jpaQueryFactory
                .select(Projections.bean(
                        BusinessScopeVO.class,
                        qBusinessScopeEntity.phid,
                        qBusinessScopeEntity.name,
                        qBusinessScopeEntity.status))
                .from(qBusinessScopeEntity)
                .where(booleanBuilder)
                .orderBy(qBusinessScopeEntity.phInsertDt.desc())
                .offset((currPage - 1) * size)
                .limit(size).fetchResults();
        PageData pageData = new PageData(results.getResults(), results.getTotal(), size, currPage);
        return ResultRes.success(pageData);
    }
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    @CacheEvict(cacheNames = {BUSINESS_SCOPE_CACHE}, allEntries = true)
    public ResultRes deleteData(IdsForm form) {
        List<Long> ids = form.getIds();
        List<BusinessScopeEntity> businessScopeEntityList = queryFactory.selectFrom(qBusinessScopeEntity)
                .where(qBusinessScopeEntity.phid.in(ids).and(qBusinessScopeEntity.phDelflag.eq(0L)))
                .fetch();
        if(CollectionUtils.isEmpty(businessScopeEntityList)){
            throw new BusinessException("数据不存在,请检查!");
        }
        queryFactory.update(qBusinessScopeEntity)
                .set(qBusinessScopeEntity.phDelflag, 1L)
                .set(qBusinessScopeEntity.phUpdateDt, LocalDateTime.now())
                .where(qBusinessScopeEntity.phid.in(ids))
                .execute();
        return ResultRes.success();
    }
}

常用属性说明

cacheNames/value :用来指定缓存组件的名字

key :缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写)

keyGenerator :key 的生成器。 key 和 keyGenerator 二选一使用

cacheManager :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。

condition :可以用来指定符合条件的情况下才缓存

unless :否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过 #result 获取方法结果)

sync :是否使用异步模式。

spEL 编写 key

前面说过,缓存的 key 支持使用 spEL 表达式去编写,下面总结一下使用 spEL 去编写 key 可以用的一些元数据:

以上就是详解如何使用SpringBoot的缓存@Cacheable的详细内容,更多关于SpringBoot缓存@Cacheable的资料请关注脚本之家其它相关文章!

相关文章

  • Mybatis配置文件之动态SQL配置备忘录

    Mybatis配置文件之动态SQL配置备忘录

    这篇文章主要介绍了Mybatis配置文件之动态SQL配置备忘录的相关资料,需要的朋友可以参考下
    2017-05-05
  • JMM核心概念之Happens-before原则

    JMM核心概念之Happens-before原则

    关于Java并发的通信机制是基于共享内存实现的,线程之间共享程序的公共状态,通过写-读内存中的公共状态进行隐式通信,这对程序员是透明的,我们需要理解其工作机制,以防止内存可见性问题,从而编写出正确同步的代码
    2021-06-06
  • 详解java 三种调用机制(同步、回调、异步)

    详解java 三种调用机制(同步、回调、异步)

    这篇文章主要介绍了java 三种调用机制(同步、回调、异步),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • java map中相同的key保存多个value值方式

    java map中相同的key保存多个value值方式

    这篇文章主要介绍了java map中相同的key保存多个value值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • RocketMQ集群消费与广播消费模式

    RocketMQ集群消费与广播消费模式

    这篇文章主要介绍了RocketMQ集群消费与广播消费模式,消息队列RocketMQ版支持集群消费和广播消费,本文介绍集群消费和广播消费的基本概念、适用场景、功能差异、注意事项以及设置方式
    2023-02-02
  • Java 图文并茂讲解主方法中的String[] args参数作用

    Java 图文并茂讲解主方法中的String[] args参数作用

    很多老铁不清楚JAVA主方法中main()里面的的参数是什么意思,以及有什么作用,接下来给大家用最通俗易懂的话来讲解,还不清楚的朋友来看看吧
    2022-04-04
  • Java实现螺旋矩阵的示例

    Java实现螺旋矩阵的示例

    这篇文章主要介绍了Java实现螺旋矩阵的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • SpringBoot+Spring Security无法实现跨域的解决方案

    SpringBoot+Spring Security无法实现跨域的解决方案

    这篇文章主要介绍了SpringBoot+Spring Security无法实现跨域的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 基于Java SSM框架实现简易的评教系统

    基于Java SSM框架实现简易的评教系统

    这篇文章主要介绍了通过Java SSM框架实现一个简易的评教系统的示例代码,文中的代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-02-02
  • 解决Idea运行junit测试时报Error:[3,17] 程序包org.junit不存在的问题

    解决Idea运行junit测试时报Error:[3,17] 程序包org.junit不存在的问题

    这篇文章主要介绍了Idea运行junit测试时报Error:[3,17] 程序包org.junit不存在解决方法,本文给大家分享两种解决办法,需要的朋友可以参考下
    2023-03-03

最新评论