SpringBoot实现缓存预热的几种常用方案

🏷️个人主页牵着猫散步的鼠鼠

🏷️系列专栏Java全栈-专栏

🏷️个人学习笔记,若有缺误,欢迎评论区指正
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站AI学习网站

目录

前言

实现方案概述

具体实现方案

[① 启动监听事件](#① 启动监听事件)

[② @PostConstruct 注解](#② @PostConstruct 注解)

[③ CommandLineRunner或ApplicationRunner](#③ CommandLineRunner或ApplicationRunner)

[④ 实现InitializingBean接口](#④ 实现InitializingBean接口)

小结

​编辑


前言

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

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

实现方案概述

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

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

具体实现方案

① 启动监听事件

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

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

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

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

② @PostConstruct 注解

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

java 复制代码
@Component
public class CachePreloader {
    
    @Autowired
    private YourCacheManager cacheManager;

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

③ CommandLineRunner或ApplicationRunner

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

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

ApplicationRunner 实现示例如下:

java 复制代码
@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 初始化完成后执行缓存预热,具体实现代码如下:

java 复制代码
@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 接口的方式来完成。

相关推荐
这里有鱼汤几秒前
一文读懂量化交易中最常用的5种均线,附源码,建议收藏
后端·python
风象南33 分钟前
SpringBoot的5种签到打卡实现方案
java·spring boot·后端
追逐时光者36 分钟前
C#/.NET/.NET Core技术前沿周刊 | 第 41 期(2025年6.1-6.8)
后端·.net
追逐时光者42 分钟前
不写一行代码 .NET 使用 FluentCMS 快速构建现代化内容管理系统(CMS)
后端·.net·cms
华清远见成都中心1 小时前
大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计
人工智能·缓存·语言模型
星辰离彬1 小时前
Java 高级泛型实战:8 个场景化编程技巧
java·开发语言·后端·程序人生
张哈大2 小时前
【 java 虚拟机知识 第一篇 】
java·开发语言·jvm·笔记·缓存
懒虫虫~5 小时前
基于SpringBoot利用死信队列解决RabbitMQ业务队列故障重试无效场景问题
spring boot·rabbitmq
伤不起bb7 小时前
Redis 哨兵模式
数据库·redis·缓存
迪迦不喝可乐7 小时前
Redis 知识点一
redis·缓存