springboot 缓存预热的几种方案

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

这里我给大家总结几个缓存预热的方案。

方案1:使用启动监听事件实现缓存预热

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

监听 ContextRefreshedEvent事件

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);
    }
}

方案2:使用 @PostConstruct 注解实现缓存预热

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

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

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

方案3:使用 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。

方案4:通过实现 InitializingBean 接口,并重写 afterPropertiesSet 方法实现缓存预热

实现 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);
    }
}

总结

  1. 使用启动监听事件实现缓存预热
    优点:可以在应用完全启动之前执行,可以确保缓存预热在所有依赖初始化完成之后进行。
    缺点:处理复杂,需要对Spring的事件机制有一定了解。
  2. 使用@PostConstruct注解实现缓存预热
    优点:简单易用,不需要额外的接口实现,适用于简单的预热逻辑。
    缺点:对于复杂的预热逻辑,可能会导致方法变得臃肿,不易于维护。
  3. 使用CommandLineRunner或ApplicationRunner实现缓存预热
    优点:非常灵活,适合处理复杂的预热逻辑,可以接受参数,易于测试和扩展。
    缺点:可能不如@PostConstruct直观,对于非常简单的预热逻辑可能显得有些过度设计。
  4. 通过实现InitializingBean接口,并重写afterPropertiesSet方法实现缓存预热
    优点:这是Spring推荐的方式之一,保证了bean的生命周期管理,适合需要在属性注入完毕后进行初始化的场景。
    缺点:对于非Spring Bean的类不适用,且对于简单的预热逻辑,可能会觉得有些繁琐。

推荐:

  1. 如果你的预热逻辑较为简单,且希望保持代码简洁,推荐@PostConstruct注解。
  2. 对于更复杂的情况,尤其是需要接收参数或执行更复杂的业务逻辑时,使用CommandLineRunner或ApplicationRunner会更加合适,它提供了更多的灵活性和控制。
  3. 如果你正在处理的是一个Spring Bean,并且需要在属性注入完成后执行预热逻辑,那么实现InitializingBean接口是标准且推荐的做法。

所以比较推荐后两种方案。

相关推荐
醇醛酸醚酮酯6 小时前
多线程是如何保证数据一致和MESI缓存一致性协议
缓存
代码老y1 天前
穿透、误伤与回环——Redis 缓存防御体系的负向路径与治理艺术
数据库·redis·缓存
Code季风1 天前
深度优化 spring 性能:从缓存、延迟加载到并发控制的实战指南
java·spring boot·后端·spring·缓存·性能优化
Themberfue1 天前
Redis ①⑥-缓存
数据库·redis·adb·缓存
Hello.Reader1 天前
RedisJSON 路径语法深度解析与实战
数据库·redis·缓存
千宇宙航1 天前
闲庭信步使用图像验证平台加速FPGA的开发:第十课——图像gamma矫正的FPGA实现
图像处理·计算机视觉·缓存·fpga开发
Alfred king1 天前
面试150 LRU缓存
链表·缓存·哈希表·lru·双向链表
岸边的风2 天前
退出登录后头像还在?这个缓存问题坑过多少前端!
前端·缓存·状态模式
Liudef062 天前
大模型KV缓存量化误差补偿机制:提升推理效率的关键技术
人工智能·缓存
在未来等你2 天前
Redis面试精讲 Day 1:Redis核心特性与应用场景
数据库·redis·缓存·nosql·面试准备