網站首頁 編程語言 正文
很多時候我們代碼中使用緩存時都是先判斷緩存中沒有數(shù)據(jù)我們再讀取數(shù)據(jù)庫而有則直接使用緩存數(shù)據(jù),而在系統(tǒng)冷啟動(當系統(tǒng)重啟或新啟動時,緩存是空的,這被稱為冷啟動)時,我們毫無意外都是直接獲取數(shù)據(jù)庫的內容,這時候緩存的命中率幾乎為0,這時候我們需要考慮業(yè)務系統(tǒng)的緩存預熱功能,在系統(tǒng)啟動之前通過預先將常用數(shù)據(jù)加載到緩存中,以提高緩存命中率和系統(tǒng)性能的過程。緩存預熱的目的是盡可能地避免緩存擊穿和緩存雪崩。
一、系統(tǒng)啟動時加載
1、CommandLineRunner和ApplicationRunner是SpringBoot中用于在應用程序啟動后執(zhí)行特定邏輯的接口。
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方式完成緩存預熱");
}
}
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、實現(xiàn)InitializingBean接口,并在afterPropertiesSet方法中執(zhí)行緩存預熱的邏輯。這樣Spring在初始化Bean時會調用afterPropertiesSet方法
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,我們可以在應用程序完全啟動并處于可用狀態(tài)后執(zhí)行一些初始化邏輯。使用@EventListener注解或實現(xiàn)ApplicationListener接口來監(jiān)聽這個事件。
4、@PostConstruct注解標注一個方法,該方法將在 Bean 的構造函數(shù)執(zhí)行完畢后立即被調用。
這里3、4點的例子寫在一起
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種方式的加載順序,可根據(jù)自己的需求選擇對應的方式
?
二、定時任務加載
使用Spring的@Scheduled,quartz,xxl-job都可以,@Scheduled為例
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1點執(zhí)行
public void scheduledCachePreload() {
//TODO 緩存預熱邏輯
log.info("定時任務方式完成緩存預熱");
}
原文鏈接:https://blog.csdn.net/LittleMangoYX/article/details/136447593
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-22 pyecharts繪制時間輪播圖柱形圖+餅圖+玫瑰圖+折線圖_python
- 2022-07-18 Linux 文件內容瀏覽;cut命令;uniq命令;sort命令;tr命令;
- 2023-04-07 C語言中循環(huán)嵌套的應用方式_C 語言
- 2021-11-29 Docker部署前后端分離項目的實現(xiàn)示例_docker
- 2022-07-09 利用Python上傳日志并監(jiān)控告警的方法詳解_python
- 2022-08-13 socket:REUSEADDR與REUSEPORT選項
- 2022-03-29 詳解python字符串相關str_python
- 2022-03-23 Unity3d實現(xiàn)跑馬燈廣播效果_C#教程
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支