Springboot實(shí)現(xiàn)緩存預(yù)熱的方法
很多時(shí)候我們代碼中使用緩存時(shí)都是先判斷緩存中沒有數(shù)據(jù)我們再讀取數(shù)據(jù)庫而有則直接使用緩存數(shù)據(jù),而在系統(tǒng)冷啟動(dòng)(當(dāng)系統(tǒng)重啟或新啟動(dòng)時(shí),緩存是空的,這被稱為冷啟動(dòng))時(shí),我們毫無意外都是直接獲取數(shù)據(jù)庫的內(nèi)容,這時(shí)候緩存的命中率幾乎為0,這時(shí)候我們需要考慮業(yè)務(wù)系統(tǒng)的緩存預(yù)熱功能,在系統(tǒng)啟動(dòng)之前通過預(yù)先將常用數(shù)據(jù)加載到緩存中,以提高緩存命中率和系統(tǒng)性能的過程。緩存預(yù)熱的目的是盡可能地避免緩存擊穿和緩存雪崩。
一、系統(tǒng)啟動(dòng)時(shí)加載
1、CommandLineRunner和ApplicationRunner是SpringBoot中用于在應(yīng)用程序啟動(dòng)后執(zhí)行特定邏輯的接口。
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * 緩存預(yù)熱器(CommandLineRunner方式) */ @Component @Slf4j public class CacheCLRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { //TODO 緩存預(yù)熱邏輯 log.info("CommandLineRunner方式完成緩存預(yù)熱"); } }
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; /** * 緩存預(yù)熱器(ApplicationRunner方式) */ @Component @Slf4j public class CacheAppRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { //TODO 緩存預(yù)熱邏輯 log.info("ApplicationRunner方式完成緩存預(yù)熱"); } }
2、實(shí)現(xiàn)InitializingBean接口,并在afterPropertiesSet方法中執(zhí)行緩存預(yù)熱的邏輯。這樣Spring在初始化Bean時(shí)會(huì)調(diào)用afterPropertiesSet方法
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; /** * 緩存預(yù)熱器(InitializingBean方式) */ @Component @Slf4j public class CacheInitializing implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { //TODO 緩存預(yù)熱邏輯 log.info("InitializingBean方式完成緩存預(yù)熱"); } }
3、基于ApplicationReadyEvent,我們可以在應(yīng)用程序完全啟動(dòng)并處于可用狀態(tài)后執(zhí)行一些初始化邏輯。使用@EventListener注解或?qū)崿F(xiàn)ApplicationListener接口來監(jiān)聽這個(gè)事件。
4、@PostConstruct注解標(biāo)注一個(gè)方法,該方法將在 Bean 的構(gòu)造函數(shù)執(zhí)行完畢后立即被調(diào)用。
這里3、4點(diǎn)的例子寫在一起
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; /** * 緩存預(yù)熱器(ApplicationReadyEvent和@PostConstruct方式) */ @Component @Slf4j public class CacheLoader { @EventListener(ApplicationReadyEvent.class) public void loadCache1() { //TODO 緩存預(yù)熱邏輯 log.info("@EventListener方式完成緩存預(yù)熱"); } @PostConstruct public void loadCache2() { //TODO 緩存預(yù)熱邏輯 log.info("@PostConstruct方式完成緩存預(yù)熱"); } }
啟動(dòng)項(xiàng)目大致看一下4種方式的加載順序,可根據(jù)自己的需求選擇對應(yīng)的方式
二、定時(shí)任務(wù)加載
使用Spring的@Scheduled,quartz,xxl-job都可以,@Scheduled為例
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1點(diǎn)執(zhí)行 public void scheduledCachePreload() { //TODO 緩存預(yù)熱邏輯 log.info("定時(shí)任務(wù)方式完成緩存預(yù)熱"); }
更多java知識請點(diǎn)擊下方名片!
到此這篇關(guān)于Springboot實(shí)現(xiàn)緩存預(yù)熱的文章就介紹到這了,更多相關(guān)Springboot緩存預(yù)熱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot讀取templates文件html代碼實(shí)例
這篇文章主要介紹了Springboot讀取templates文件html代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)
下面小編就為大家?guī)硪黄狫ava數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Java函數(shù)式接口Supplier接口實(shí)例詳解
這篇文章主要介紹了Java函數(shù)式接口Supplier接口實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02詳解mybatis-plus使用@EnumValue注解的方式對枚舉類型的處理
這篇文章主要介紹了詳解mybatis-plus使用@EnumValue注解的方式對枚舉類型的處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java httpclient請求form-data格式并設(shè)置boundary代碼實(shí)現(xiàn)方法
在 Java 開發(fā)中,經(jīng)常會(huì)遇到需要使用 httpclient 發(fā)送 form-data 格式請求的場景,本文將詳細(xì)介紹如何正確地實(shí)現(xiàn)這一操作,包括數(shù)據(jù)格式示例、常見報(bào)錯(cuò)及解決方法,本文通過實(shí)例代碼詳解講解,感興趣的朋友一起看看吧2025-01-01