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

SpringBoot如何實現(xiàn)緩存預(yù)熱?

開發(fā) 架構(gòu)
在 Spring Boot 項目啟動之后,在什么時候?在哪里可以將數(shù)據(jù)加載到緩存系統(tǒng)呢?

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

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

實現(xiàn)方案概述

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

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

具體實現(xiàn)方案

1.啟動監(jiān)聽事件

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

在需要進行緩存預(yù)熱的類上添加 @Component 注解,并在其方法中添加 @PostConstruct 注解和緩存預(yù)熱的業(yè)務(wù)邏輯,具體實現(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)用程序啟動后要執(zhí)行的接口,它們都允許我們在應(yīng)用啟動后執(zhí)行一些自定義的初始化邏輯,例如緩存預(yù)熱。CommandLineRunner 實現(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 實現(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 接口有一個 run(String... args) 方法,它接收命令行參數(shù)作為可變長度字符串數(shù)組。
  • ApplicationRunner 接口則提供了一個 run(ApplicationArguments args) 方法,它接收一個 ApplicationArguments 對象作為參數(shù),這個對象提供了對傳入的所有命令行參數(shù)(包括選項和非選項參數(shù))的訪問。

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

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

使用場景不同

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

4.實現(xiàn)InitializingBean接口

實現(xiàn) InitializingBean 接口并重寫 afterPropertiesSet 方法,可以在 Spring Bean 初始化完成后執(zhí)行緩存預(yù)熱,具體實現(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 項目啟動時,預(yù)先將數(shù)據(jù)加載到緩存系統(tǒng)(如 Redis)中的一種機制。它可以通過監(jiān)聽 ContextRefreshedEvent 或 ApplicationReadyEvent 啟動事件,或使用 @PostConstruct 注解,或?qū)崿F(xiàn) CommandLineRunner 接口、ApplicationRunner 接口,和 InitializingBean 接口的方式來完成。

責任編輯:姜華 來源: 磊哥和Java
相關(guān)推薦

2024-10-28 07:15:00

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

2024-01-19 14:03:59

Redis緩存系統(tǒng)Spring

2024-10-15 16:01:19

SpringBoot緩存預(yù)熱

2021-11-04 08:04:49

緩存CaffeineSpringBoot

2024-04-07 00:00:02

Redis雪崩緩存

2023-02-14 07:47:20

SpringBootEhcache

2025-02-11 08:23:41

2024-10-31 11:52:05

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

2022-06-17 07:49:14

緩存LRU

2023-10-12 22:38:18

SpringBoot熱部署

2024-01-03 21:50:32

緩存機制請求

2024-10-09 10:46:41

springboot緩存redis

2023-10-12 08:00:48

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2023-05-05 18:38:33

多級緩存Caffeine開發(fā)

2024-11-01 10:37:31

2019-11-11 15:33:34

高并發(fā)緩存數(shù)據(jù)

2019-08-14 15:08:51

緩存存儲數(shù)據(jù)

2021-07-13 06:57:12

SpringbootAOP緩存

2025-04-21 03:00:00

點贊
收藏

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