springboot+redis實(shí)現(xiàn)微博熱搜排行榜的示例代碼
技術(shù)模擬思路:
采用26個(gè)英文字母來實(shí)現(xiàn)排行,隨機(jī)為每個(gè)字母生成一個(gè)隨機(jī)數(shù)作為score
為了更好的體驗(yàn),先做幾件事:
- 先初始化1個(gè)月的歷史數(shù)據(jù)
- 定時(shí)5秒鐘,模擬微博的熱度刷新(例如模擬點(diǎn)贊 收藏 評(píng)論的熱度值更新)
- 定時(shí)1小時(shí)合并統(tǒng)計(jì) 天、周、月的排行榜。
步驟1:先初始化1個(gè)月的歷史數(shù)據(jù)
@Service
@Slf4j
public class InitService {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? /**
? ? ?* 先初始化1個(gè)月的歷史數(shù)據(jù)
? ? ?*/
? ? public void init30day(){
? ? ? ? //計(jì)算當(dāng)前的小時(shí)key
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //初始化近30天,每天24個(gè)key
? ? ? ? for(int i=1;i<24*30;i++){
? ? ? ? ? ? //倒推過去30天
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? this.initMember(key);
? ? ? ? ? ? System.out.println(key);
? ? ? ? }
? ? }
? ? /**
? ? ?*初始化某個(gè)小時(shí)的key
? ? ?*/
? ? public void initMember(String key) {
? ? ? ? Random rand = new Random();
? ? ? ? //采用26個(gè)英文字母來實(shí)現(xiàn)排行,隨機(jī)為每個(gè)字母生成一個(gè)隨機(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í)刷新數(shù)據(jù)
@Service
@Slf4j
public class TaskService {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? /**
? ? ?*2. 定時(shí)5秒鐘,模擬微博的熱度刷新(例如模擬點(diǎn)贊 收藏 評(píng)論的熱度值更新)
? ? ?* 3. 定時(shí)1小時(shí)合并統(tǒng)計(jì) 天、周、月的排行榜。
? ? ?*/
? ? @PostConstruct
? ? public void init(){
? ? ? ? log.info("啟動(dòng)初始化 ..........");
// ? ? ? ?2. 定時(shí)5秒鐘,模擬微博的熱度刷新(例如模擬點(diǎn)贊 收藏 評(píng)論的熱度值更新)
? ? ? ? new Thread(()->this.refreshDataHour()).start();
// ? ? ? ?3. 定時(shí)1小時(shí)合并統(tǒng)計(jì) 天、周、月的排行榜。
? ? ? ? new Thread(()->this.refreshData()).start();
? ? }
? ? /**
? ? ?*采用26個(gè)英文字母來實(shí)現(xiàn)排行,隨機(jī)為每個(gè)字母生成一個(gè)隨機(jī)數(shù)作為score
? ? ?*/
? ? public void refreshHour(){
? ? ? ? //計(jì)算當(dāng)前的小時(shí)key
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? //為26個(gè)英文字母來實(shí)現(xiàn)排行,隨機(jī)為每個(gè)字母生成一個(gè)隨機(jī)數(shù)作為score
? ? ? ? Random rand = new Random();
? ? ? ? for(int i = 1;i<=26;i++){
? ? ? ? ? ? //redis的ZINCRBY 新增這個(gè)積分值
? ? ? ? ? ? this.redisTemplate.opsForZSet().incrementScore(Constants.HOUR_KEY+hour,String.valueOf((char)(96+i)),rand.nextInt(10));
? ? ? ? }
? ? }
? ? /**
? ? ?*刷新當(dāng)天的統(tǒng)計(jì)數(shù)據(jù)
? ? ?*/
? ? public void refreshDay(){
? ? ? ? long hour=System.currentTimeMillis()/(1000*60*60);
? ? ? ? List<String> otherKeys=new ArrayList<>();
? ? ? ? //算出近24小時(shí)內(nèi)的key
? ? ? ? for(int i=1;i<23;i++){
? ? ? ? ? ? String ?key=Constants.HOUR_KEY+(hour-i);
? ? ? ? ? ? otherKeys.add(key);
? ? ? ? }
? ? ? ? //把當(dāng)前的時(shí)間key,并且把后推23個(gè)小時(shí),共計(jì)近24小時(shí),求出并集存入Constants.DAY_KEY中
? ? ? ? //redis ZUNIONSTORE 求并集
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.DAY_KEY);
? ? ? ? //設(shè)置當(dāng)天的key 40天過期,不然歷史數(shù)據(jù)浪費(fèi)內(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)計(jì)數(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)前的時(shí)間key,并且把后推24*7-1個(gè)小時(shí),共計(jì)近24*7小時(shí),求出并集存入Constants.WEEK_KEY中
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.WEEK_KEY);
? ? ? ? log.info("周刷新完成..........");
? ? }
? ? /**
? ? ?*刷新30天的統(tǒng)計(jì)數(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)前的時(shí)間key,并且把后推24*30個(gè)小時(shí),共計(jì)近24*30小時(shí),求出并集存入Constants.MONTH_KEY中
? ? ? ? this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.MONTH_KEY);
? ? ? ? log.info("月刷新完成..........");
? ? }
? ? /**
? ? ?*定時(shí)1小時(shí)合并統(tǒng)計(jì) 天、周、月的排行榜。
? ? ?*/
? ? public void refreshData(){
? ? ? ? while (true){
? ? ? ? ? ? //刷新當(dāng)天的統(tǒng)計(jì)數(shù)據(jù)
? ? ? ? ? ? this.refreshDay();
// ? ? ? ? ? ?刷新7天的統(tǒng)計(jì)數(shù)據(jù)
? ? ? ? ? ? this.refreshWeek();
// ? ? ? ? ? ?刷新30天的統(tǒng)計(jì)數(shù)據(jù)
? ? ? ? ? ? this.refreshMonth();
? ? ? ? ? ? //TODO 在分布式系統(tǒng)中,建議用xxljob來實(shí)現(xiàn)定時(shí)
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(1000*60*60);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?*定時(shí)5秒鐘,模擬微博的熱度刷新(例如模擬點(diǎn)贊 收藏 評(píng)論的熱度值更新)
? ? ?*/
? ? public void refreshDataHour(){
? ? ? ? while (true){
? ? ? ? ? ? this.refreshHour();
? ? ? ? ? ? //TODO 在分布式系統(tǒng)中,建議用xxljob來實(shí)現(xiàn)定時(shí)
? ? ? ? ? ? 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實(shí)現(xiàn)微博熱搜排行榜的示例代碼的文章就介紹到這了,更多相關(guān)springboot redis微博熱搜排行榜內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot基于AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換
本文主要介紹了SpringBoot基于AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
java利用DFA算法實(shí)現(xiàn)敏感詞過濾功能
在最近的開發(fā)中遇到了敏感詞過濾,便去網(wǎng)上查閱了很多敏感詞過濾的資料,在這里也和大家分享一下自己的理解。下面這篇文章主要給大家介紹了關(guān)于java利用DFA算法實(shí)現(xiàn)敏感詞過濾功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Springboot+Redis實(shí)現(xiàn)API接口防刷限流的項(xiàng)目實(shí)踐
本文主要介紹了Springboot+Redis實(shí)現(xiàn)API接口防刷限流的項(xiàng)目實(shí)踐,通過限流可以讓系統(tǒng)維持在一個(gè)相對(duì)穩(wěn)定的狀態(tài),為更多的客戶提供服務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Liquibase 在 Spring Boot 中的使用詳細(xì)介紹
Liquibase 提供了靈活的變更集機(jī)制,支持創(chuàng)建表、修改列、填充數(shù)據(jù)、回滾變更等多種操作,本文將通過多個(gè)豐富的示例,詳細(xì)講解如何在 Spring Boot項(xiàng)目中使用 Liquibase,感興趣的朋友跟隨小編一起看看吧2024-12-12
Spring事務(wù)隔離級(jí)別簡介及實(shí)例解析
這篇文章主要介紹了Spring事務(wù)隔離級(jí)別簡介及實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
基于Calendar獲取當(dāng)前時(shí)間的性能比較
這篇文章主要介紹了Calendar獲取當(dāng)前時(shí)間的性能比較,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

