SpringBoot?緩存預熱的實現(xiàn)
簡介:
SpringBoot集合RedisUtil和 CommadnLinRunner實現(xiàn)緩存預熱
一、新建一個緩存抽象類
在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();
}
}二、 新建一個組件
項目啟動之前,預先加載數(shù)據(jù)。 比如,權(quán)限容器、特殊用戶數(shù)據(jù)等。通常我們可以使用監(jiān)聽器、事件來操作。 但是,springboot提供了一個簡單的方式來實現(xiàn)此類需求,即,CommandLineRunner。
這是一個接口,用戶可以自定義實現(xiàn)該接口,具體實現(xiàn)run方法
任何在上下文容器之內(nèi)的bean都可以實現(xiàn)run方法
如果在上下文中,存在多個該接口實現(xiàn)類,可以通過@order注解,指定加載順序

@Component
public class InitCache implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 獲取Springshang上下文對象
ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
// 獲取目標接口下的所有實現(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()) {
// 通過ApplicationContext的getBean方法來獲取Spring容器中已初始化的bean
AbstractCache abstractCache = (AbstractCache) SpringContextUtil.getBean(entry.getValue().getClass());
// 緩存實現(xiàn)類 調(diào)用緩存初始方法
abstractCache.initCache();
}
}
}三、準備工具類
3.1 RedisUtil
/**
* Redis工具類
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存儲 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 上下文對象
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
// 通過ApplicationContext的getBean方法來獲取Spring容器中已初始化的bean
public static Object getBean(Class type) {
return applicationContext.getBean(type);
}
}四、新建緩存實現(xiàn)類
在用戶模塊 新建3個實現(xiàn)類
4.1 ClassCache
/**
* 班級緩存
*/
@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", "一年級一班");
}
@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
/**
* 學科緩存
*/
@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("目錄", "化學");
}
@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
/**
* 學生緩存
*/
@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);
}
}五、測試



到此這篇關(guān)于SpringBoot 緩存預熱的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 緩存預熱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot配置連接兩個或多個數(shù)據(jù)庫的實現(xiàn)
本文主要介紹了SpringBoot配置連接兩個或多個數(shù)據(jù)庫的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
解讀java?try?catch?異常后還會繼續(xù)執(zhí)行嗎
這篇文章主要介紹了解讀java?try?catch?異常后還會不會繼續(xù)執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
springboot用戶數(shù)據(jù)修改的詳細實現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于springboot用戶數(shù)據(jù)修改的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04

