springboot+redis實現(xiàn)微博熱搜排行榜的示例代碼
技術(shù)模擬思路:
采用26個英文字母來實現(xiàn)排行,隨機(jī)為每個字母生成一個隨機(jī)數(shù)作為score
為了更好的體驗,先做幾件事:
- 先初始化1個月的歷史數(shù)據(jù)
- 定時5秒鐘,模擬微博的熱度刷新(例如模擬點贊 收藏 評論的熱度值更新)
- 定時1小時合并統(tǒng)計 天、周、月的排行榜。
步驟1:先初始化1個月的歷史數(shù)據(jù)
@Service
@Slf4j
public class InitService {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? /**
? ? ?* 先初始化1個月的歷史數(shù)據(jù)
? ? ?*/
? ? public void init30day(){
? ? ? ? //計算當(dāng)前的小時key
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //初始化近30天,每天24個key
? ? ? ? for(int i=1;i<24*30;i++){
? ? ? ? ? ? //倒推過去30天
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? this.initMember(key);
? ? ? ? ? ? System.out.println(key);
? ? ? ? }
? ? }
? ? /**
? ? ?*初始化某個小時的key
? ? ?*/
? ? public void initMember(String key) {
? ? ? ? Random rand = new Random();
? ? ? ? //采用26個英文字母來實現(xiàn)排行,隨機(jī)為每個字母生成一個隨機(jī)數(shù)作為score
? ? ? ? for(int i = 1;i<=26;i++){
? ? ? ? ? ? this.redisTemplate.opsForZSet().add(key,String.valueOf((char)(96+i)),rand.nextInt(10));
? ? ? ? }
? ? }
}步驟2:定時刷新數(shù)據(jù)
@Service
@Slf4j
public class TaskService {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? /**
? ? ?*2. 定時5秒鐘,模擬微博的熱度刷新(例如模擬點贊 收藏 評論的熱度值更新)
? ? ?* 3. 定時1小時合并統(tǒng)計 天、周、月的排行榜。
? ? ?*/
? ? @PostConstruct
? ? public void init(){
? ? ? ? log.info("啟動初始化 ..........");
// ? ? ? ?2. 定時5秒鐘,模擬微博的熱度刷新(例如模擬點贊 收藏 評論的熱度值更新)
? ? ? ? new Thread(()->this.refreshDataHour()).start();
// ? ? ? ?3. 定時1小時合并統(tǒng)計 天、周、月的排行榜。
? ? ? ? new Thread(()->this.refreshData()).start();
? ? }
? ? /**
? ? ?*采用26個英文字母來實現(xiàn)排行,隨機(jī)為每個字母生成一個隨機(jī)數(shù)作為score
? ? ?*/
? ? public void refreshHour(){
? ? ? ? //計算當(dāng)前的小時key
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //為26個英文字母來實現(xiàn)排行,隨機(jī)為每個字母生成一個隨機(jī)數(shù)作為score
? ? ? ? Random rand = new Random();
? ? ? ? for(int i = 1;i<=26;i++){
? ? ? ? ? ? //redis的ZINCRBY 新增這個積分值
? ? ? ? ? ? this.redisTemplate.opsForZSet().incrementScore(Constants.HOUR_KEY+hour,String.valueOf((char)(96+i)),rand.nextInt(10));
? ? ? ? }
? ? }
? ? /**
? ? ?*刷新當(dāng)天的統(tǒng)計數(shù)據(jù)
? ? ?*/
? ? public void refreshDay(){
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? List<String> otherKeys=new ArrayList<>();
? ? ? ? //算出近24小時內(nèi)的key
? ? ? ? for(int i=1;i<23;i++){
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? otherKeys.add(key);
? ? ? ? }
? ? ? ? //把當(dāng)前的時間key,并且把后推23個小時,共計近24小時,求出并集存入Constants.DAY_KEY中
? ? ? ? //redis ZUNIONSTORE 求并集
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.DAY_KEY);
? ? ? ? //設(shè)置當(dāng)天的key 40天過期,不然歷史數(shù)據(jù)浪費內(nèi)存
? ? ? ? for(int i=0;i<24;i++){
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? this.redisTemplate.expire(key,40, TimeUnit.DAYS);
? ? ? ? }
? ? ? ? log.info("天刷新完成..........");
? ? }
? ? /**
? ? ?*刷新7天的統(tǒng)計數(shù)據(jù)
? ? ?*/
? ? public void refreshWeek(){
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? List<String> otherKeys=new ArrayList<>();
? ? ? ? //算出近7天內(nèi)的key
? ? ? ? for(int i=1;i<24*7-1;i++){
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? otherKeys.add(key);
? ? ? ? }
? ? ? ? //把當(dāng)前的時間key,并且把后推24*7-1個小時,共計近24*7小時,求出并集存入Constants.WEEK_KEY中
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.WEEK_KEY);
? ? ? ? log.info("周刷新完成..........");
? ? }
? ? /**
? ? ?*刷新30天的統(tǒng)計數(shù)據(jù)
? ? ?*/
? ? public void refreshMonth(){
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? List<String> otherKeys=new ArrayList<>();
? ? ? ? //算出近30天內(nèi)的key
? ? ? ? for(int i=1;i<24*30-1;i++){
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? otherKeys.add(key);
? ? ? ? }
? ? ? ? //把當(dāng)前的時間key,并且把后推24*30個小時,共計近24*30小時,求出并集存入Constants.MONTH_KEY中
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.MONTH_KEY);
? ? ? ? log.info("月刷新完成..........");
? ? }
? ? /**
? ? ?*定時1小時合并統(tǒng)計 天、周、月的排行榜。
? ? ?*/
? ? public void refreshData(){
? ? ? ? while (true){
? ? ? ? ? ? //刷新當(dāng)天的統(tǒng)計數(shù)據(jù)
? ? ? ? ? ? this.refreshDay();
// ? ? ? ? ? ?刷新7天的統(tǒng)計數(shù)據(jù)
? ? ? ? ? ? this.refreshWeek();
// ? ? ? ? ? ?刷新30天的統(tǒng)計數(shù)據(jù)
? ? ? ? ? ? this.refreshMonth();
? ? ? ? ? ? //TODO 在分布式系統(tǒng)中,建議用xxljob來實現(xiàn)定時
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(1000*60*60);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?*定時5秒鐘,模擬微博的熱度刷新(例如模擬點贊 收藏 評論的熱度值更新)
? ? ?*/
? ? public void refreshDataHour(){
? ? ? ? while (true){
? ? ? ? ? ? this.refreshHour();
? ? ? ? ? ? //TODO 在分布式系統(tǒng)中,建議用xxljob來實現(xiàn)定時
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(5000);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}步驟3:排行榜查詢接口
@RestController
@Slf4j
public class Controller {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? @GetMapping(value = "/getHour")
? ? public Set getHour() {
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //ZREVRANGE 返回有序集key中,指定區(qū)間內(nèi)的成員,降序。
? ? ? ? Set<ZSetOperations.TypedTuple<Integer>> rang= this.redisTemplate.opsForZSet().reverseRangeWithScores(Constants.HOUR_KEY+hour,0,30);
? ? ? ? return rang;
? ? }
? ? @GetMapping(value = "/getDay")
? ? public Set getDay() {
? ? ? ? Set<ZSetOperations.TypedTuple<Integer>> rang= this.redisTemplate.opsForZSet().reverseRangeWithScores(Constants.DAY_KEY,0,30);
? ? ? ? return rang;
? ? }
? ? @GetMapping(value = "/getWeek")
? ? public Set getWeek() {
? ? ? ? Set<ZSetOperations.TypedTuple<Integer>> rang= this.redisTemplate.opsForZSet().reverseRangeWithScores(Constants.WEEK_KEY,0,30);
? ? ? ? return rang;
? ? }
? ? @GetMapping(value = "/getMonth")
? ? public Set getMonth() {
? ? ? ? Set<ZSetOperations.TypedTuple<Integer>> rang= this.redisTemplate.opsForZSet().reverseRangeWithScores(Constants.MONTH_KEY,0,30);
? ? ? ? return rang;
? ? }
}到此這篇關(guān)于springboot+redis實現(xiàn)微博熱搜排行榜的示例代碼的文章就介紹到這了,更多相關(guān)springboot redis微博熱搜排行榜內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何實現(xiàn)自定義SpringBoot的Starter組件
這篇文章主要介紹了實現(xiàn)自定義SpringBoot的Starter組件的示例代碼,想要自定義starter組件,首先要了解springboot是如何加載starter的,也就是springboot的自動裝配機(jī)制原理,本文結(jié)合示例代碼詳細(xì)講解,需要的朋友可以參考下2023-02-02
Java concurrency集合之LinkedBlockingDeque_動力節(jié)點Java學(xué)院整理
LinkedBlockingDeque是雙向鏈表實現(xiàn)的雙向并發(fā)阻塞隊列。該阻塞隊列同時支持FIFO和FILO兩種操作方式,即可以從隊列的頭和尾同時操作(插入/刪除);并且,該阻塞隊列是支持線程安全。2017-06-06
Java8利用Stream實現(xiàn)列表去重的方法詳解
這篇文章主要為大家介紹了Java利用Stream實現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-04-04
win10 java(jdk安裝)環(huán)境變量配置和相關(guān)問題
這篇文章主要介紹了win10java(jdk安裝)環(huán)境變量配置和相關(guān)問題解決,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12

