springboot中redis的緩存穿透問題實現(xiàn)
什么是緩存穿透問題??
我們使用redis是為了減少數(shù)據(jù)庫的壓力,讓盡量多的請求去承壓能力比較大的redis,而不是數(shù)據(jù)庫。但是高并發(fā)條件下,可能會在redis還沒有緩存的時候,大量的請求同時進入,導(dǎo)致一大批的請求直奔數(shù)據(jù)庫,而不會經(jīng)過redis。使用代碼模擬緩存穿透問題如下:
首先是service里面的代碼:
@Service
public class NewsService {
@Autowired
private NewsDAO newsDAO;
//springboot自動初始化,不需要我們進行配置,直接注入到代碼中使用
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
public /*synchronized*/ List<News> getLatestNews(int userId,int offset,int limit){
//設(shè)置序列化方式,防止亂碼
redisTemplate.setKeySerializer(new StringRedisSerializer());
//第一步:查詢緩存
News news= (News) redisTemplate.opsForValue().get("newsKey");
//判斷是否存在緩存
if(null == news){//查詢數(shù)據(jù)庫
news = newsDAO.selectByUserIdAndOffset(userId,offset,limit).get(0);
//
redisTemplate.opsForValue().set("newsKey",news);
System.out.println("進入數(shù)據(jù)庫。。。。。。。。");
}else{
System.out.println("進入緩存。。。。。。。。。");
}
return newsDAO.selectByUserIdAndOffset(userId,offset,limit);
}
}
然后是使用線程池在Controller里面對請求進行模擬:
@Controller
public class HomeController {
@Autowired
UserService userService;
@Autowired
NewsService newsService;
//遇到的坑,如果不加method,頁面啟動不起來。
@RequestMapping(value = "/home",method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String index(Model model){
//這邊是可以讀出數(shù)據(jù)來的
//線程池------緩存穿透問題的復(fù)現(xiàn)
ExecutorService executorService = Executors.newFixedThreadPool(8*2);
for(int i = 0;i < 50000;i++){
executorService.submit(new Runnable() {
@Override
public void run() {
List<News> newsList = newsService.getLatestNews(0,0,10);
}
});
}
List<News> newsList = newsService.getLatestNews(0,0,10);
News news=newsList.get(0);
return news.getImage();
}
}
結(jié)果如圖:大量的請求進入數(shù)據(jù)庫,那么如何解決這個問題?

方法一、在方法上加鎖:
@Service
public class NewsService {
@Autowired
private NewsDAO newsDAO;
//springboot自動初始化,不需要我們進行配置,直接注入到代碼中使用
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
//第一種方式:方法加鎖
public synchronized List<News> getLatestNews(int userId,int offset,int limit){
//設(shè)置序列化方式,防止亂碼
redisTemplate.setKeySerializer(new StringRedisSerializer());
//第一步:查詢緩存
News news= (News) redisTemplate.opsForValue().get("newsKey");
//判斷是否存在緩存
if(null == news){
//查詢數(shù)據(jù)庫
news = newsDAO.selectByUserIdAndOffset(userId,offset,limit).get(0);
//
redisTemplate.opsForValue().set("newsKey",news);
System.out.println("進入數(shù)據(jù)庫。。。。。。。。");
}else{
System.out.println("進入緩存。。。。。。。。。");
}
return newsDAO.selectByUserIdAndOffset(userId,offset,limit);
}
}
直接在方法上加鎖,保證每次只有一個請求可以進入。但是這個方法存在一個缺陷,每次只有一個請求可以進入,請求處理的速度變得相當?shù)穆?,不利于系統(tǒng)的實時性。
方法二、使用雙重校驗鎖:
@Service
public class NewsService {
@Autowired
private NewsDAO newsDAO;
//springboot自動初始化,不需要我們進行配置,直接注入到代碼中使用
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
//第一種方式:方法加鎖
public /*synchronized*/ List<News> getLatestNews(int userId,int offset,int limit){
//設(shè)置序列化方式,防止亂碼
redisTemplate.setKeySerializer(new StringRedisSerializer());
//第一步:查詢緩存
News news= (News) redisTemplate.opsForValue().get("newsKey");
//判斷是否存在緩存
if(null == news){
//第二種方式:雙重檢測鎖
synchronized (this){
//查詢數(shù)據(jù)庫
news = newsDAO.selectByUserIdAndOffset(userId,offset,limit).get(0);
//
redisTemplate.opsForValue().set("newsKey",news);
System.out.println("進入數(shù)據(jù)庫。。。。。。。。");
}
}else{
System.out.println("進入緩存。。。。。。。。。");
}
return newsDAO.selectByUserIdAndOffset(userId,offset,limit);
}
}
這個方法比較好,雖然不能保證只有一個請求請求數(shù)據(jù)庫,但是當?shù)谝慌埱筮M來,第二批之后的所有請求全部會在緩存取數(shù)據(jù)。
到此這篇關(guān)于springboot中redis的緩存穿透問題實現(xiàn)的文章就介紹到這了,更多相關(guān)springboot redis緩存穿透內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Druid動態(tài)數(shù)據(jù)源配置重復(fù)刷錯誤日志的問題
使用druid數(shù)據(jù)庫連接池實現(xiàn)動態(tài)的配置數(shù)據(jù)源功能,在配置過程中出現(xiàn)一個問題既然是用戶自己配置的數(shù)據(jù)源,就無法避免輸入錯誤,連接失敗等情況,關(guān)于這個問題怎么處理呢,今天小編通過本文給大家詳細說明下,感興趣的朋友一起看看吧2021-05-05
FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法
本文主要介紹了FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Java String.replace()方法"無效"的原因及解決方式
這篇文章主要介紹了Java String.replace()方法"無效"的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Springboot中@Async異步,實現(xiàn)異步結(jié)果合并統(tǒng)一返回方式
這篇文章主要介紹了Springboot中@Async異步,實現(xiàn)異步結(jié)果合并統(tǒng)一返回方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

