springboot中redis的緩存穿透問題實(shí)現(xiàn)
什么是緩存穿透問題??
我們使用redis是為了減少數(shù)據(jù)庫的壓力,讓盡量多的請求去承壓能力比較大的redis,而不是數(shù)據(jù)庫。但是高并發(fā)條件下,可能會在redis還沒有緩存的時候,大量的請求同時進(jìn)入,導(dǎo)致一大批的請求直奔數(shù)據(jù)庫,而不會經(jīng)過redis。使用代碼模擬緩存穿透問題如下:
首先是service里面的代碼:
@Service public class NewsService { @Autowired private NewsDAO newsDAO; //springboot自動初始化,不需要我們進(jìn)行配置,直接注入到代碼中使用 @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("進(jìn)入數(shù)據(jù)庫。。。。。。。。"); }else{ System.out.println("進(jìn)入緩存。。。。。。。。。"); } return newsDAO.selectByUserIdAndOffset(userId,offset,limit); } }
然后是使用線程池在Controller里面對請求進(jìn)行模擬:
@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é)果如圖:大量的請求進(jìn)入數(shù)據(jù)庫,那么如何解決這個問題?
方法一、在方法上加鎖:
@Service public class NewsService { @Autowired private NewsDAO newsDAO; //springboot自動初始化,不需要我們進(jìn)行配置,直接注入到代碼中使用 @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("進(jìn)入數(shù)據(jù)庫。。。。。。。。"); }else{ System.out.println("進(jìn)入緩存。。。。。。。。。"); } return newsDAO.selectByUserIdAndOffset(userId,offset,limit); } }
直接在方法上加鎖,保證每次只有一個請求可以進(jìn)入。但是這個方法存在一個缺陷,每次只有一個請求可以進(jìn)入,請求處理的速度變得相當(dāng)?shù)穆?,不利于系統(tǒng)的實(shí)時性。
方法二、使用雙重校驗(yàn)鎖:
@Service public class NewsService { @Autowired private NewsDAO newsDAO; //springboot自動初始化,不需要我們進(jìn)行配置,直接注入到代碼中使用 @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("進(jìn)入數(shù)據(jù)庫。。。。。。。。"); } }else{ System.out.println("進(jìn)入緩存。。。。。。。。。"); } return newsDAO.selectByUserIdAndOffset(userId,offset,limit); } }
這個方法比較好,雖然不能保證只有一個請求請求數(shù)據(jù)庫,但是當(dāng)?shù)谝慌埱筮M(jìn)來,第二批之后的所有請求全部會在緩存取數(shù)據(jù)。
到此這篇關(guān)于springboot中redis的緩存穿透問題實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot redis緩存穿透內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組完整實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組的相關(guān)資料,這道題來自力扣,通過學(xué)習(xí)這道題的解題思路以及代碼對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01SpringBoot任意版本集成Swagger各種版本的操作指南
在學(xué)習(xí)Swagger生成API文檔的時候經(jīng)常會遇到問題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項(xiàng)目上卻無法使用,這是一個經(jīng)常性的問題,本文章就旨在和大家搞定SpringBoot任意版本集成Swagger各種版本2024-07-07java如何發(fā)送get請求獲取數(shù)據(jù)(附代碼)
這篇文章主要給大家介紹了關(guān)于java如何發(fā)送get請求獲取數(shù)據(jù)的相關(guān)資料,Java中的GET請求方法是HTTP協(xié)議中的一種請求方式,用于向服務(wù)器請求獲取資源,需要的朋友可以參考下2023-10-10Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解
這篇文章主要介紹了Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11基于Java 生產(chǎn)者消費(fèi)者模式(詳細(xì)分析)
下面小編就為大家分享一篇基于Java 生產(chǎn)者消費(fèi)者模式(詳細(xì)分析),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Java利用iTextPDF庫實(shí)現(xiàn)制作PDF表格模板并填充數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了如何通過Java的iTextPDF庫制作一個PDF表格模板并填充數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-12-12