SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)
簡(jiǎn)介:
SpringBoot集合RedisUtil和 CommadnLinRunner實(shí)現(xiàn)緩存預(yù)熱
一、新建一個(gè)緩存抽象類
在redis模塊里面 新建
/**
* 緩存抽象類
*/
@Component
public abstract class AbstractCache {
// 初始化緩存
public void initCache() {
}
public <T> T getCache(String key) {
return null;
}
// 清除緩存
public void clearCache() {
}
// 加載緩存
public void reloadCache() {
clearCache();
initCache();
}
}二、 新建一個(gè)組件
項(xiàng)目啟動(dòng)之前,預(yù)先加載數(shù)據(jù)。 比如,權(quán)限容器、特殊用戶數(shù)據(jù)等。通常我們可以使用監(jiān)聽(tīng)器、事件來(lái)操作。 但是,springboot提供了一個(gè)簡(jiǎn)單的方式來(lái)實(shí)現(xiàn)此類需求,即,CommandLineRunner。
這是一個(gè)接口,用戶可以自定義實(shí)現(xiàn)該接口,具體實(shí)現(xiàn)run方法
任何在上下文容器之內(nèi)的bean都可以實(shí)現(xiàn)run方法
如果在上下文中,存在多個(gè)該接口實(shí)現(xiàn)類,可以通過(guò)@order注解,指定加載順序

@Component
public class InitCache implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 獲取Springshang上下文對(duì)象
ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
// 獲取目標(biāo)接口下的所有實(shí)現(xiàn)類
Map<String, AbstractCache> beanMap = applicationContext.getBeansOfType(AbstractCache.class);
// 調(diào)用init方法
if (beanMap.isEmpty()) {
return;
}
for (Map.Entry<String, AbstractCache> entry : beanMap.entrySet()) {
// 通過(guò)ApplicationContext的getBean方法來(lái)獲取Spring容器中已初始化的bean
AbstractCache abstractCache = (AbstractCache) SpringContextUtil.getBean(entry.getValue().getClass());
// 緩存實(shí)現(xiàn)類 調(diào)用緩存初始方法
abstractCache.initCache();
}
}
}三、準(zhǔn)備工具類
3.1 RedisUtil
/**
* Redis工具類
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存儲(chǔ) key value
* @param key
* @param value
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 判斷是否存在 key
* @param key
* @return
*/
public Boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
}3.2 SpringContextUtil
/**
* Spring 容器工具類
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// 靜態(tài)方法 提供Spring 上下文對(duì)象
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
// 通過(guò)ApplicationContext的getBean方法來(lái)獲取Spring容器中已初始化的bean
public static Object getBean(Class type) {
return applicationContext.getBean(type);
}
}四、新建緩存實(shí)現(xiàn)類
在用戶模塊 新建3個(gè)實(shí)現(xiàn)類
4.1 ClassCache
/**
* 班級(jí)緩存
*/
@Component
public class ClassCache extends AbstractCache {
@Autowired
private RedisUtil redisUtil;
private static final String CLASS_CACHE_KEY = "CLASS";
@Autowired
private RedisTemplate redisTemplate;
@Override
public void initCache() {
redisUtil.set("classId", "一年級(jí)一班");
}
@Override
public <T> T getCache(String key) {
if (!redisTemplate.hasKey(key).booleanValue()) {
reloadCache();
}
return (T) redisTemplate.opsForValue().get(key);
}
@Override
public void clearCache() {
redisTemplate.delete(CLASS_CACHE_KEY);
}
}4.2 SubjectCache
/**
* 學(xué)科緩存
*/
@Component
public class SubjectCache extends AbstractCache {
@Autowired
private RedisUtil redisUtil;
private static final String SUBJECT_CACHE_KEY = "SUBJECT";
@Autowired
private RedisTemplate redisTemplate;
@Override
public void initCache() {
redisUtil.set("目錄", "化學(xué)");
}
@Override
public <T> T getCache(String key) {
if (!redisTemplate.hasKey(key).booleanValue()) {
reloadCache();
}
return (T) redisTemplate.opsForValue().get(key);
}
@Override
public void clearCache() {
redisTemplate.delete(SUBJECT_CACHE_KEY);
}
}4.3 SysUserCache
/**
* 學(xué)生緩存
*/
@Component
public class SysUserCache extends AbstractCache {
@Autowired
private RedisUtil redisUtil;
private static final String SYS_USER_CACHE_KEY = "SYS_USER";
@Autowired
private RedisTemplate redisTemplate;
@Override
public void initCache() {
redisUtil.set("name", "杰克");
}
@Override
public <T> T getCache(String key) {
if (!redisTemplate.hasKey(key).booleanValue()) {
reloadCache();
}
return (T) redisTemplate.opsForValue().get(key);
}
@Override
public void clearCache() {
redisTemplate.delete(SYS_USER_CACHE_KEY);
}
}五、測(cè)試



到此這篇關(guān)于SpringBoot 緩存預(yù)熱的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 緩存預(yù)熱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)
本文主要介紹了SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
java啟動(dòng)參數(shù)之謎的排查過(guò)程
在日常操作中,相信很多人對(duì)Java啟動(dòng)參數(shù)存在疑惑,下面這篇文章主要給大家介紹了關(guān)于java啟動(dòng)參數(shù)之謎的排查過(guò)程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
解讀java?try?catch?異常后還會(huì)繼續(xù)執(zhí)行嗎
這篇文章主要介紹了解讀java?try?catch?異常后還會(huì)不會(huì)繼續(xù)執(zhí)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
springboot用戶數(shù)據(jù)修改的詳細(xì)實(shí)現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于springboot用戶數(shù)據(jù)修改的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Java中CAS機(jī)制實(shí)現(xiàn)方法詳解
傳統(tǒng)的并發(fā)控制手段如synchronized和ReentrantLock雖有效防止資源競(jìng)爭(zhēng),卻可能引起性能開(kāi)銷,相比之下,CAS(CompareAndSwap)提供一種輕量級(jí)的樂(lè)觀鎖策略,通過(guò)硬件級(jí)別的原子指令實(shí)現(xiàn)無(wú)鎖并發(fā),提高性能,需要的朋友可以參考下2024-09-09

