日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot+Redis(实现处理热点数据)

發(fā)布時間:2024/4/15 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot+Redis(实现处理热点数据) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、什么是熱點數(shù)據(jù)

在很短的時間內(nèi),許多數(shù)據(jù)要被多次查詢(像雙十一購物,查詢商品)

二、為什么要使用redis

redis是非關(guān)系型數(shù)據(jù)庫,Redis將數(shù)據(jù)存儲在內(nèi)存上,避免了頻繁的IO操作

三、場景

短時間內(nèi)有大量的請求來獲取用戶列表的數(shù)據(jù),每次都需要從數(shù)據(jù)庫進行查詢。

處理熱點數(shù)據(jù)的基本思路:用戶首次訪問從數(shù)據(jù)庫中取值,之后每次進行判斷只要redis中有數(shù)據(jù),就從redis中取值,其中涉及到多線程訪問造成的內(nèi)存穿透問題,采用雙重檢查的形式解決。

package com.abbey.myblog.service;import com.abbey.myblog.mapper.UserMapper; import com.abbey.myblog.entity.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Service;/*** @author lianying* @create 2020-11-21 9:02 下午**/@Service public class UserService {@Autowiredprivate RedisTemplate<Object, Object> redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@AutowiredUserMapper userMapper;public String Sel(int id){String user = stringRedisTemplate.opsForValue().get(""+id);// 防止首次訪問該接口有大量用戶,造成內(nèi)存穿透,使redis沒有效果if(null == user){synchronized (this){if(null == user){System.out.println("從mysql中查詢數(shù)據(jù)中。。。。。。");// 從數(shù)據(jù)庫中查詢數(shù)據(jù)user = userMapper.Sel(id).toString();System.err.println(user);// 放入redisstringRedisTemplate.opsForValue().set(""+id,user);}}}else{System.out.println("從redis中查詢數(shù)據(jù)中。。。。。。");}return user;} }

項目源碼地址:https://github.com/Barry-Manilow/myblog

總結(jié)

以上是生活随笔為你收集整理的SpringBoot+Redis(实现处理热点数据)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。