自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試官:SpringBoot如何實(shí)現(xiàn)緩存預(yù)熱?

數(shù)據(jù)庫 Redis
在 Spring Boot 項(xiàng)目啟動(dòng)之后,在什么時(shí)候?在哪里可以將數(shù)據(jù)加載到緩存系統(tǒng)呢?

緩存預(yù)熱是指在 Spring Boot 項(xiàng)目啟動(dòng)時(shí),預(yù)先將數(shù)據(jù)加載到緩存系統(tǒng)(如 Redis)中的一種機(jī)制。

那么問題來了,在 Spring Boot 項(xiàng)目啟動(dòng)之后,在什么時(shí)候?在哪里可以將數(shù)據(jù)加載到緩存系統(tǒng)呢?

實(shí)現(xiàn)方案概述

在 Spring Boot 啟動(dòng)之后,可以通過以下手段實(shí)現(xiàn)緩存預(yù)熱:

  • 使用啟動(dòng)監(jiān)聽事件實(shí)現(xiàn)緩存預(yù)熱。
  • 使用 @PostConstruct 注解實(shí)現(xiàn)緩存預(yù)熱。
  • 使用 CommandLineRunner 或 ApplicationRunner 實(shí)現(xiàn)緩存預(yù)熱。
  • 通過實(shí)現(xiàn) InitializingBean 接口,并重寫 afterPropertiesSet 方法實(shí)現(xiàn)緩存預(yù)熱。

具體實(shí)現(xiàn)方案

1、啟動(dòng)監(jiān)聽事件

可以使用 ApplicationListener 監(jiān)聽 ContextRefreshedEvent 或 ApplicationReadyEvent 等應(yīng)用上下文初始化完成事件,在這些事件觸發(fā)后執(zhí)行數(shù)據(jù)加載到緩存的操作,具體實(shí)現(xiàn)如下:

@Component
public class CacheWarmer implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

或監(jiān)聽 ApplicationReadyEvent 事件,如下代碼所示:

@Component
public class CacheWarmer implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

2、@PostConstruct 注解

在需要進(jìn)行緩存預(yù)熱的類上添加 @Component 注解,并在其方法中添加 @PostConstruct 注解和緩存預(yù)熱的業(yè)務(wù)邏輯,具體實(shí)現(xiàn)代碼如下:

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

    @PostConstruct
    public void preloadCache() {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

3、CommandLineRunner或ApplicationRunner

CommandLineRunner 和 ApplicationRunner 都是 Spring Boot 應(yīng)用程序啟動(dòng)后要執(zhí)行的接口,它們都允許我們在應(yīng)用啟動(dòng)后執(zhí)行一些自定義的初始化邏輯,例如緩存預(yù)熱。CommandLineRunner 實(shí)現(xiàn)示例如下:

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

ApplicationRunner 實(shí)現(xiàn)示例如下:

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

CommandLineRunner 和 ApplicationRunner 區(qū)別如下:

方法簽名不同:

  • CommandLineRunner 接口有一個(gè) run(String... args) 方法,它接收命令行參數(shù)作為可變長度字符串?dāng)?shù)組。
  • ApplicationRunner 接口則提供了一個(gè) run(ApplicationArguments args) 方法,它接收一個(gè) ApplicationArguments 對象作為參數(shù),這個(gè)對象提供了對傳入的所有命令行參數(shù)(包括選項(xiàng)和非選項(xiàng)參數(shù))的訪問。

參數(shù)解析方式不同:

  • CommandLineRunner 接口更簡單直接,適合處理簡單的命令行參數(shù)。
  • ApplicationRunner 接口提供了一種更強(qiáng)大的參數(shù)解析能力,可以通過 ApplicationArguments 獲取詳細(xì)的參數(shù)信息,比如獲取選項(xiàng)參數(shù)及其值、非選項(xiàng)參數(shù)列表以及查詢是否存在特定參數(shù)等。

使用場景不同:

  • 當(dāng)只需要處理一組簡單的命令行參數(shù)時(shí),可以使用 CommandLineRunner。
  • 對于需要精細(xì)控制和解析命令行參數(shù)的復(fù)雜場景,推薦使用 ApplicationRunner。

4、實(shí)現(xiàn)InitializingBean接口

實(shí)現(xiàn) InitializingBean 接口并重寫 afterPropertiesSet 方法,可以在 Spring Bean 初始化完成后執(zhí)行緩存預(yù)熱,具體實(shí)現(xiàn)代碼如下:

@Component
public class CachePreloader implements InitializingBean {
    @Autowired
    private YourCacheManager cacheManager;
    @Override
    public void afterPropertiesSet() throws Exception {
        // 執(zhí)行緩存預(yù)熱業(yè)務(wù)...
        cacheManager.put("key", dataList);
    }
}

小結(jié)

緩存預(yù)熱是指在 Spring Boot 項(xiàng)目啟動(dòng)時(shí),預(yù)先將數(shù)據(jù)加載到緩存系統(tǒng)(如 Redis)中的一種機(jī)制。它可以通過監(jiān)聽 ContextRefreshedEvent 或 ApplicationReadyEvent 啟動(dòng)事件,或使用 @PostConstruct 注解,或?qū)崿F(xiàn) CommandLineRunner 接口、ApplicationRunner 接口,和 InitializingBean 接口的方式來完成。

責(zé)任編輯:姜華 來源: Java中文社群
相關(guān)推薦

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2024-12-18 17:20:07

緩存預(yù)熱緩存系統(tǒng)Spring

2024-10-28 07:15:00

SpringBoot緩存預(yù)熱數(shù)據(jù)加載

2025-03-17 00:00:00

2024-03-18 14:06:00

停機(jī)Spring服務(wù)器

2023-11-20 10:09:59

2024-09-11 22:51:19

線程通訊Object

2024-01-26 13:16:00

RabbitMQ延遲隊(duì)列docker

2024-04-09 10:40:04

2024-10-22 16:39:07

2015-08-13 10:29:12

面試面試官

2024-03-12 10:44:42

2021-05-20 08:54:16

Go面向對象

2021-12-15 06:58:13

List 集合LinkedHashS

2024-02-04 10:08:34

2021-05-19 06:07:21

CSS 斜線效果技巧

2021-05-20 08:34:03

CDN原理網(wǎng)絡(luò)

2021-10-26 10:29:45

掃碼登錄功能

2024-09-09 15:09:30

2024-12-25 15:44:15

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)