首页 > 其他分享 >缓存预热是指在 Spring Boot 项目启动时

缓存预热是指在 Spring Boot 项目启动时

时间:2024-02-02 17:33:21浏览次数:18  
标签:... CommandLineRunner 缓存 Spring Boot 预热 ApplicationRunner public

缓存预热是指在 Spring Boot 项目启动时,预先将数据加载到缓存系统(如 Redis)中的一种机制。

那么问题来了,在 Spring Boot 项目启动之后,在什么时候?在哪里可以将数据加载到缓存系统呢?

实现方案概述

在 Spring Boot 启动之后,可以通过以下手段实现缓存预热:

  1. 使用启动监听事件实现缓存预热。
  2. 使用 @PostConstruct 注解实现缓存预热。
  3. 使用 CommandLineRunner 或 ApplicationRunner 实现缓存预热。
  4. 通过实现 InitializingBean 接口,并重写 afterPropertiesSet 方法实现缓存预热。

具体实现方案

① 启动监听事件

可以使用 ApplicationListener 监听 ContextRefreshedEvent 或 ApplicationReadyEvent 等应用上下文初始化完成事件,在这些事件触发后执行数据加载到缓存的操作,具体实现如下:

@Component
public class CacheWarmer implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

或监听 ApplicationReadyEvent 事件,如下代码所示:

@Component
public class CacheWarmer implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

② @PostConstruct 注解

在需要进行缓存预热的类上添加 @Component 注解,并在其方法中添加 @PostConstruct 注解和缓存预热的业务逻辑,具体实现代码如下:

@Component
public class CachePreloader {
    
    @Autowired
    private YourCacheManager cacheManager;

    @PostConstruct
    public void preloadCache() {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

③ CommandLineRunner或ApplicationRunner

CommandLineRunner 和 ApplicationRunner 都是 Spring Boot 应用程序启动后要执行的接口,它们都允许我们在应用启动后执行一些自定义的初始化逻辑,例如缓存预热。CommandLineRunner 实现示例如下:

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

ApplicationRunner 实现示例如下:

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

CommandLineRunner 和 ApplicationRunner 区别如下:

  1. 方法签名不同

    • CommandLineRunner 接口有一个 run(String... args) 方法,它接收命令行参数作为可变长度字符串数组。
    • ApplicationRunner 接口则提供了一个 run(ApplicationArguments args) 方法,它接收一个 ApplicationArguments 对象作为参数,这个对象提供了对传入的所有命令行参数(包括选项和非选项参数)的访问。
  2. 参数解析方式不同

    • CommandLineRunner 接口更简单直接,适合处理简单的命令行参数。
    • ApplicationRunner 接口提供了一种更强大的参数解析能力,可以通过 ApplicationArguments 获取详细的参数信息,比如获取选项参数及其值、非选项参数列表以及查询是否存在特定参数等。
  3. 使用场景不同

    • 当只需要处理一组简单的命令行参数时,可以使用 CommandLineRunner。
    • 对于需要精细控制和解析命令行参数的复杂场景,推荐使用 ApplicationRunner。

④ 实现InitializingBean接口

实现 InitializingBean 接口并重写 afterPropertiesSet 方法,可以在 Spring Bean 初始化完成后执行缓存预热,具体实现代码如下:

@Component
public class CachePreloader implements InitializingBean {
    @Autowired
    private YourCacheManager cacheManager;
    @Override
    public void afterPropertiesSet() throws Exception {
        // 执行缓存预热业务...
        cacheManager.put("key", dataList);
    }
}

小结

缓存预热是指在 Spring Boot 项目启动时,预先将数据加载到缓存系统(如 Redis)中的一种机制。它可以通过监听 ContextRefreshedEvent 或 ApplicationReadyEvent 启动事件,或使用 @PostConstruct 注解,或实现 CommandLineRunner 接口、ApplicationRunner 接口,和 InitializingBean 接口的方式来完成。

标签:...,CommandLineRunner,缓存,Spring,Boot,预热,ApplicationRunner,public
From: https://www.cnblogs.com/hefeng2014/p/18003571

相关文章

  • SpringMVC的执行流程及初始化流程
    今天大致来看一下SpringMVC的执行流程和初始化流程是什么样的1,执行流程:也就是一个请求是怎么到我们Controller的2,初始化流程:也就是那些HandlerMapping、HandlerAdapter是怎么初始化并让我们拿到的执行流程我们都知道DispatcherServlet(前端控制器)这样的一个类,是这个类来帮......
  • SpringBoot读取配置文件的几种方式
    示例user:name:zhaotianage:18sex:男@Value注解@Value注解是Spring框架提供的用于注入配置属性值的注解,它可用于类的成员变量、方法参数和构造函数参数上。@Data@ComponentpublicclassMyBean{@Value("${user.name}")privateStringname;@V......
  • ssm三大框架和springboot有什么关系?
    SSM框架是指Spring+SpringMVC+MyBatis的组合,它们分别是Java开发中常用的三个框架。而SpringBoot(管家)是基于Spring框架的一种快速开发框架。更具体地说,SSM框架是一种传统的JavaWeb开发框架组合,其中:Spring是一个全功能的企业级Java开发框架,提供了依赖注入......
  • springboot+redis实现登录
    1.登录成功后响应jwt,同时将令牌存入redis2.拦截器中不光校验jwt,从redis取出,对比校验springboot集成redis1.导入redis起步依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependenc......
  • springboot上传图片到阿里云
    1.登录注册 购买oss服务2.创建Bucket3.获取accesskey4.编写阿里云上传工具类publicclassAliOssUtil{privatestaticfinalStringENDPOINT="https://oss-cn-beijing.aliyuncs.com";privatestaticfinalStringACCESS_KEY_ID="填写自己的accesskey";......
  • Spring Cloud Config核心功能和原理解析
    配置管理的前世今生随着技术的发展,配置项管理变得越来越简单,尽管如今它只限于管理业务属性或者配置初始化参数等等,但是当年它可肩负着SpringIOC的光荣使命,风光无限。想当年刚入行的时候还是SSH(Struts+Spring+Hibernate)的天下,那时远没有如今这些丰富的开源组件,一个标准的......
  • 第17天:信息打点-语言框架&开发组件&FastJson&Shiro&Log4j&SpringBoot等
    框架:简单代码的一个整合库,如果使用框架就只需要学习使用框架调用即可如:文件上传功能是需要很多代码来实现的,框架把这个代码进行封封装,调用即可影响:如果采用框架开发,代码的安全性是取决于框架的过滤机制 #Python-开发框架-Django&FlaskDjango1、识别插件2、Set-Cookie:expi......
  • SpringBoot自动化配置原理
    SpringBoot自动化配置从注解@SpringBootApplication开始,它封装的注解如下图所示:需要注意的有三个注解:1.第一个注解是@SpringBootConfiguration,底层是一个@Configuration注解,表示当前类是一个配置类,可以使得引导类中的SpringBoot或Spring配置能生效2.第二个注解是@ComponentSc......
  • SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据实测!
    开发目的: 提高百万级数据插入效率。采取方案: 利用ThreadPoolTaskExecutor多线程批量插入。采用技术: springboot2.1.1+mybatisPlus3.0.6+swagger2.5.0+Lombok1.18.4+postgresql+ThreadPoolTaskExecutor等。application-dev.properties添加线程池配置信息#异步线程配置#配置核......
  • SpringBoot的自动化配置原理
    1.启动类上有一个注解,是一个复合注解,由三个注解组成第一个注解是@SpringBootConfiguration,底层是一个@Configuration注解,表示当前类是一个配置类第二个注解是@ComponentScan是一个组件扫描,spring会扫描引导类所在包及子包下的组件第三个注解是@EnableAutoConfiguration注......