SpringBoot如何實(shí)現(xiàn)緩存預(yù)熱
緩存預(yù)熱是指在 Spring Boot 項(xiàng)目啟動(dòng)時(shí),預(yù)先將數(shù)據(jù)加載到緩存系統(tǒng)(如 Redis)中的一種機(jī)制。
那么問(wèn)題來(lái)了,在 Spring Boot 項(xiàng)目啟動(dòng)之后,在什么時(shí)候?在哪里可以將數(shù)據(jù)加載到緩存系統(tǒng)呢?
實(shí)現(xiàn)方案概述
在 Spring Boot 啟動(dòng)之后,可以通過(guò)以下手段實(shí)現(xiàn)緩存預(yù)熱:
1、使用啟動(dòng)監(jiān)聽(tīng)事件實(shí)現(xiàn)緩存預(yù)熱。
2、使用 @PostConstruct 注解實(shí)現(xiàn)緩存預(yù)熱。
3、使用 CommandLineRunner 或 ApplicationRunner 實(shí)現(xiàn)緩存預(yù)熱。
4、通過(guò)實(shí)現(xiàn) InitializingBean 接口,并重寫(xiě) afterPropertiesSet 方法實(shí)現(xiàn)緩存預(yù)熱。
具體實(shí)現(xiàn)方案
① 啟動(dòng)監(jiān)聽(tīng)事件
可以使用 ApplicationListener 監(jiān)聽(tīng) 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)聽(tīng) ApplicationReadyEvent 事件,如下代碼所示:
@Component public class CacheWarmer implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 執(zhí)行緩存預(yù)熱業(yè)務(wù)... cacheManager.put("key", dataList); } }
② @PostConstruct 注解
在需要進(jìn)行緩存預(yù)熱的類(lèi)上添加 @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); } }
③ CommandLineRunner或ApplicationRunner
CommandLineRunner 和 ApplicationRunner 都是 Spring Boot 應(yīng)用程序啟動(dòng)后要執(zhí)行的接口,它們都允許我們?cè)趹?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ū)別如下:
1、方法簽名不同:
CommandLineRunner 接口有一個(gè) run(String… args) 方法,它接收命令行參數(shù)作為可變長(zhǎng)度字符串?dāng)?shù)組。
ApplicationRunner 接口則提供了一個(gè) run(ApplicationArguments args) 方法,它接收一個(gè) ApplicationArguments 對(duì)象作為參數(shù),這個(gè)對(duì)象提供了對(duì)傳入的所有命令行參數(shù)(包括選項(xiàng)和非選項(xiàng)參數(shù))的訪(fǎng)問(wèn)。
2、參數(shù)解析方式不同:
CommandLineRunner 接口更簡(jiǎn)單直接,適合處理簡(jiǎn)單的命令行參數(shù)。
ApplicationRunner 接口提供了一種更強(qiáng)大的參數(shù)解析能力,可以通過(guò) ApplicationArguments 獲取詳細(xì)的參數(shù)信息,比如獲取選項(xiàng)參數(shù)及其值、非選項(xiàng)參數(shù)列表以及查詢(xún)是否存在特定參數(shù)等。
3、使用場(chǎng)景不同:
當(dāng)只需要處理一組簡(jiǎn)單的命令行參數(shù)時(shí),可以使用 CommandLineRunner。
對(duì)于需要精細(xì)控制和解析命令行參數(shù)的復(fù)雜場(chǎng)景,推薦使用 ApplicationRunner。
④ 實(shí)現(xiàn)InitializingBean接口
實(shí)現(xiàn) InitializingBean 接口并重寫(xiě) 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ī)制。它可以通過(guò)監(jiān)聽(tīng) ContextRefreshedEvent 或 ApplicationReadyEvent 啟動(dòng)事件,或使用 @PostConstruct 注解,或?qū)崿F(xiàn) CommandLineRunner 接口、ApplicationRunner 接口,和 InitializingBean 接口的方式來(lái)完成。
到此這篇關(guān)于SpringBoot如何實(shí)現(xiàn)緩存預(yù)熱的文章就介紹到這了,更多相關(guān)SpringBoot緩存預(yù)熱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot自定義校驗(yàn)注解的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了springboot自定義校驗(yàn)注解的實(shí)現(xiàn)過(guò)程,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11java網(wǎng)上圖書(shū)商城(5)購(gòu)物車(chē)模塊2
這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上圖書(shū)商城,購(gòu)物車(chē)模塊第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12maven項(xiàng)目test執(zhí)行main找不到資源文件的問(wèn)題及解決
這篇文章主要介紹了maven項(xiàng)目test執(zhí)行main找不到資源文件的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Thread類(lèi)interrupt interrupted及isInterrupted區(qū)別
這篇文章主要為大家介紹了Thread類(lèi)interrupt interrupted及isInterrupted區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)踐指南
在 Java 開(kāi)發(fā)中,許多場(chǎng)景需要訪(fǎng)問(wèn)多個(gè)數(shù)據(jù)庫(kù),例如多租戶(hù)系統(tǒng)或讀寫(xiě)分離架構(gòu),為了靈活高效地管理這些場(chǎng)景,動(dòng)態(tài)數(shù)據(jù)源切換技術(shù)應(yīng)運(yùn)而生,所以本文給大家介紹了Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)踐指南,需要的朋友可以參考下2025-03-03Java實(shí)現(xiàn)簡(jiǎn)單汽車(chē)租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單汽車(chē)租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01java鏈表的常見(jiàn)簡(jiǎn)單面試算法題詳解
文章總結(jié):本文主要介紹了單鏈表的基本操作,包括頭插法、尾插法、鏈表翻轉(zhuǎn)、鏈表成環(huán)判斷、成環(huán)位置判斷、成環(huán)長(zhǎng)度判斷,以及有序鏈表的合并,通過(guò)實(shí)例和代碼示例,詳細(xì)講解了每種操作的原理和實(shí)現(xiàn)方法2025-01-01maven倉(cāng)庫(kù)repositories和mirrors的配置及區(qū)別詳解
這篇文章主要介紹了maven倉(cāng)庫(kù)repositories和mirrors的配置及區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07