springboot+redis實現(xiàn)微博熱搜排行榜的示例代碼
更新時間:2022年05月18日 14:57:34 作者:INGNIGHT
本文主要介紹了springboot+redis實現(xiàn)微博熱搜排行榜的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
技術(shù)模擬思路:
采用26個英文字母來實現(xiàn)排行,隨機為每個字母生成一個隨機數(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(){
? ? ? ? //計算當前的小時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)排行,隨機為每個字母生成一個隨機數(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)排行,隨機為每個字母生成一個隨機數(shù)作為score
? ? ?*/
? ? public void refreshHour(){
? ? ? ? //計算當前的小時key
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //為26個英文字母來實現(xiàn)排行,隨機為每個字母生成一個隨機數(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));
? ? ? ? }
? ? }
? ? /**
? ? ?*刷新當天的統(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);
? ? ? ? }
? ? ? ? //把當前的時間key,并且把后推23個小時,共計近24小時,求出并集存入Constants.DAY_KEY中
? ? ? ? //redis ZUNIONSTORE 求并集
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.DAY_KEY);
? ? ? ? //設(shè)置當天的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);
? ? ? ? }
? ? ? ? //把當前的時間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);
? ? ? ? }
? ? ? ? //把當前的時間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){
? ? ? ? ? ? //刷新當天的統(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)文章
SpringBoot基于AbstractRoutingDataSource實現(xiàn)多數(shù)據(jù)源動態(tài)切換
本文主要介紹了SpringBoot基于AbstractRoutingDataSource實現(xiàn)多數(shù)據(jù)源動態(tài)切換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
Springboot+Redis實現(xiàn)API接口防刷限流的項目實踐
本文主要介紹了Springboot+Redis實現(xiàn)API接口防刷限流的項目實踐,通過限流可以讓系統(tǒng)維持在一個相對穩(wěn)定的狀態(tài),為更多的客戶提供服務(wù),具有一定的參考價值,感興趣的可以了解一下2024-07-07
Liquibase 在 Spring Boot 中的使用詳細介紹
Liquibase 提供了靈活的變更集機制,支持創(chuàng)建表、修改列、填充數(shù)據(jù)、回滾變更等多種操作,本文將通過多個豐富的示例,詳細講解如何在 Spring Boot項目中使用 Liquibase,感興趣的朋友跟隨小編一起看看吧2024-12-12

