Springboot实现缓存预热

很多时候我们代码中使用缓存时都是先判断缓存中没有数据我们再读取数据库而有则直接使用缓存数据,而在系统冷启动(当系统重启或新启动时,缓存是空的,这被称为冷启动)时,我们毫无意外都是直接获取数据库的内容,这时候缓存的命中率几乎为0,这时候我们需要考虑业务系统的缓存预热功能,在系统启动之前通过预先将常用数据加载到缓存中,以提高缓存命中率和系统性能的过程。缓存预热的目的是尽可能地避免缓存击穿和缓存雪崩。

一、系统启动时加载

1、CommandLineRunner和ApplicationRunner是SpringBoot中用于在应用程序启动后执行特定逻辑的接口。

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 缓存预热器(CommandLineRunner方式)
 */
@Component
@Slf4j
public class CacheCLRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        //TODO 缓存预热逻辑
        log.info("CommandLineRunner方式完成缓存预热");
    }
}
java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 缓存预热器(ApplicationRunner方式)
 */
@Component
@Slf4j
public class CacheAppRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //TODO 缓存预热逻辑
        log.info("ApplicationRunner方式完成缓存预热");
    }
}

2、实现InitializingBean接口,并在afterPropertiesSet方法中执行缓存预热的逻辑。这样Spring在初始化Bean时会调用afterPropertiesSet方法

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * 缓存预热器(InitializingBean方式)
 */
@Component
@Slf4j
public class CacheInitializing implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        //TODO 缓存预热逻辑
        log.info("InitializingBean方式完成缓存预热");
    }
}

3、基于ApplicationReadyEvent,我们可以在应用程序完全启动并处于可用状态后执行一些初始化逻辑。使用@EventListener注解或实现ApplicationListener接口来监听这个事件。

4、@PostConstruct注解标注一个方法,该方法将在 Bean 的构造函数执行完毕后立即被调用。

这里3、4点的例子写在一起

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 缓存预热器(ApplicationReadyEvent和@PostConstruct方式)
 */
@Component
@Slf4j
public class CacheLoader {

    @EventListener(ApplicationReadyEvent.class)
    public void loadCache1() {
        //TODO 缓存预热逻辑
        log.info("@EventListener方式完成缓存预热");
    }

    @PostConstruct
    public void loadCache2() {
        //TODO 缓存预热逻辑
        log.info("@PostConstruct方式完成缓存预热");
    }
}

启动项目大致看一下4种方式的加载顺序,可根据自己的需求选择对应的方式

二、定时任务加载

使用Spring的@Scheduled,quartz,xxl-job都可以,@Scheduled为例

java 复制代码
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
public void scheduledCachePreload() {
	//TODO 缓存预热逻辑
	log.info("定时任务方式完成缓存预热");
}
相关推荐
菜鸟起航ing4 分钟前
【Java面试系列】Spring Boot微服务架构下的分布式事务解决方案与性能优化详解 - 3-5年Java开发必备知识
java·spring boot·微服务·性能优化·分布式事务
小薛博客14 分钟前
架构设计之Redisson分布式锁-可重入同步锁(一)
java·redis·分布式
小开不是小可爱22 分钟前
leetcode_454. 四数相加 II_java
java·数据结构·算法·leetcode
Koma-forever22 分钟前
java设计模式-原型模式
java·设计模式·原型模式
magic 2451 小时前
MyBatis的缓存、逆向工程、使用PageHelper、使用PageHelper
java·spring·maven·mybatis
XiaoLeisj1 小时前
【图书管理系统】深入解析基于 MyBatis 数据持久化操作:全栈开发图书管理系统:查询图书属性接口(注解实现)、修改图书属性接口(XML 实现)
xml·java·数据库·spring boot·sql·java-ee·mybatis
癞皮狗不赖皮2 小时前
WEB攻防-Java安全&JNDI&RMI&LDAP&五大不安全组件&RCE执行&不出网&不回显
java·jndi注入·rce代码执行
喵手2 小时前
开启多个线程,如果保证顺序执行,你知道有哪几种方式实现?
java·后端·java ee
奔跑吧邓邓子2 小时前
使用 Spring Boot 和 Uniapp 搭建 NFC 读取系统
spring boot·uni-app·nfc数据读取
斜月2 小时前
springboot3与mybatisplus3.5.5 升级实践
spring boot·后端