詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法
技術(shù)棧:
- SpringBoot 3.0.2
- Redis4
- MySQL8
- Mybatis-Plus 3.5.3.1
- Druid 1.2.18
工具:
- lombok
什么是緩存預(yù)熱?
緩存預(yù)熱是一種在程序啟動(dòng)或緩存失效之后,主動(dòng)將熱點(diǎn)數(shù)據(jù)加載到緩存中的策略。 這樣,在實(shí)際請(qǐng)求到達(dá)程序時(shí),熱點(diǎn)數(shù)據(jù)已經(jīng)存在于緩存中,從而減少了緩存穿透和緩存擊穿的情況,也緩解了SQL服務(wù)器的壓力。
實(shí)現(xiàn)
緩存抽象類
首先我們先來實(shí)現(xiàn)一個(gè)緩存抽象類,這個(gè)抽象類的作用就是在將來我們需要將某個(gè)模塊的數(shù)據(jù)需要提前加載到緩存中的時(shí)候,我們可以創(chuàng)建一個(gè)它的實(shí)現(xiàn)類,來進(jìn)行數(shù)據(jù)的緩存與加載,具體使用方式請(qǐng)看后邊我寫的例子。
public abstract class AbstractCache {
/**
* 緩存
*/
protected abstract void init();
/**
* 獲取緩存
*
* @param <T>
* @return
*/
public abstract <T> T get();
/**
* 清理緩存
*/
public abstract void clear();
/**
* 重新加載
*/
public void reload() {
clear();
init();
}
}Spring上下文工具類
接下來我們實(shí)現(xiàn)一個(gè)Spring的上下文工具類,這個(gè)工具類需要實(shí)現(xiàn)ApplicationContextAware,作用就是負(fù)責(zé)管理bean的加載與實(shí)例化的,具體如何使用,請(qǐng)往下繼續(xù)閱讀。
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
/**
* 獲取上下文
* @return
*/
public static ApplicationContext getContext() {
return applicationContext;
}
}緩存預(yù)熱
然后我們來實(shí)現(xiàn),在程序啟動(dòng)后,直接進(jìn)行數(shù)據(jù)的緩存加載,這個(gè)類需要實(shí)現(xiàn)CommandLineRunner接口,這個(gè)接口提供的方法作用就是在程序啟動(dòng)后自動(dòng)運(yùn)行。這個(gè)實(shí)現(xiàn)類里,我們使用ApplicationContextUtil工具類來獲取上下文,然后通過getBeansOfType方法獲取實(shí)現(xiàn)AbstractCache抽象類的子類,返回的是一個(gè)Map類型的集合,接下來通過getBean方法以多態(tài)的方式實(shí)例化子類,最后我們調(diào)用抽象類的init方法即可。如果有多個(gè)實(shí)現(xiàn)類,使用@Order注解標(biāo)注先后運(yùn)行就可以了。
@Component
@ConditionalOnProperty(name = {"cache.init.enable"}, havingValue = "true", matchIfMissing = false)
public class CachePreheatHandler implements CommandLineRunner {
/**
* 緩存預(yù)熱
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
ApplicationContext context = ApplicationContextUtil.getContext();
Map<String, AbstractCache> beansOfType = context.getBeansOfType(AbstractCache.class);
for (Map.Entry<String, AbstractCache> cacheEntry : beansOfType.entrySet()) {
AbstractCache cache = context.getBean(cacheEntry.getValue().getClass());
cache.init();
}
}
}解釋:
@ConditionalOnProperty這個(gè)注解在這里的作用是,需要在配置文件開啟cache.init.enable,理想值是true,默認(rèn)值是false。
cache.init.enable=true
使用
我們就以新聞熱點(diǎn)為例,數(shù)據(jù)庫中有一張tb_news新聞表,均為微博熱搜體育榜內(nèi)容。

接下來創(chuàng)建一個(gè)AbstractCache的實(shí)現(xiàn)類,來實(shí)現(xiàn)具體的實(shí)現(xiàn)
@Component
@RequiredArgsConstructor
public class NewsCache extends AbstractCache {
private static final String NEWS_KEY = "news";
private final RedisTemplate<String, Object> redisTemplate;
private final NewsService newsService;
@Override
protected void init() {
if (Boolean.FALSE.equals(redisTemplate.hasKey(NEWS_KEY))) {
redisTemplate.opsForValue().set(NEWS_KEY, newsService.list(), 30, TimeUnit.MINUTES);
}
}
@Override
public <T> T get() {
if (Boolean.FALSE.equals(redisTemplate.hasKey(NEWS_KEY))) {
reload();
}
return (T) redisTemplate.opsForValue().get(NEWS_KEY);
}
@Override
public void clear() {
redisTemplate.delete(NEWS_KEY);
}
}然后啟動(dòng)項(xiàng)目,我們就發(fā)現(xiàn),Redis中已經(jīng)存好了熱點(diǎn)數(shù)據(jù)

最后可以通過get方法獲取數(shù)據(jù)了,也不用擔(dān)心數(shù)據(jù)過期了。
@RestController
@RequestMapping("/news")
@RequiredArgsConstructor
public class NewsController {
private final NewsCache newsCache;
@GetMapping("/cache")
public List<News> list() {
return newsCache.get();
}
}好了,小伙伴們,今天的分享就到此結(jié)束了,歡迎留出建議,如果覺得內(nèi)容可以,還請(qǐng)來個(gè)點(diǎn)贊和關(guān)注吧!
以上就是詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于Redis緩存預(yù)熱的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Redis Sorted Set類型使用及應(yīng)用場景
Sorted Set是Redis常用的一種是數(shù)據(jù)類型,本文主要介紹了Redis Sorted Set類型使用及應(yīng)用場景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Window下對(duì)Redis進(jìn)行開啟與關(guān)閉的操作方法
這篇文章主要介紹了Window下對(duì)Redis進(jìn)行開啟與關(guān)閉的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
Redis存儲(chǔ)斷點(diǎn)續(xù)傳文件狀態(tài)的最佳實(shí)踐
在斷點(diǎn)續(xù)傳系統(tǒng)中,如何高效地存儲(chǔ)和更新文件上傳狀態(tài)是關(guān)鍵,得益于 Redis 高效的內(nèi)存操作和多種數(shù)據(jù)結(jié)構(gòu)的支持,它非常適合用于存儲(chǔ)上傳過程中的臨時(shí)狀態(tài)信息,下面,我們將探討如何利用 Redis 實(shí)現(xiàn)文件上傳狀態(tài)的存儲(chǔ),需要的朋友可以參考下2024-12-12

