欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 用redis的zset实现日榜,周榜,月榜

用redis的zset实现日榜,周榜,月榜

2025/2/25 6:37:46 来源:https://blog.csdn.net/m0_72410274/article/details/143798879  浏览:    关键词:用redis的zset实现日榜,周榜,月榜

思路:

请添加图片描述

1.初始化一个月的数据:

 /*** 初始化一个月数据*/@Testpublic void initMonthData(){//计算当前时间小时的keylong hour=System.currentTimeMillis()/(1000*60*60);for(int i=1;i<24*30;i++){String key="W_hour"+(hour-i);Random random =new Random();for(int j=1;j<=26;j++){stringRedisTemplate.opsForZSet().add(key,"player"+j, random.nextDouble());}}}

2.代码实现:

@RestController
@RequestMapping("/testW")
@Slf4j
public class testW {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@PostConstructpublic  void init(){log.info("定时任务启动");//定时五秒刷新点赞次数new Thread(()->this.refreshDataHour()).start();//定时一小时合并日榜 周榜月榜new Thread(()->this.refreshData()).start();}/*** 获取日榜前topN**/@PostMapping("/getDayTopN")public Set<ZSetOperations.TypedTuple<String>> getDayTopN(int topN) {return  this.stringRedisTemplate.opsForZSet().reverseRangeWithScores("W_day", 0, topN - 1);}/*** 获取周榜前topN*/@PostMapping("/getWeekTopN")public Set<ZSetOperations.TypedTuple<String>> getWeekTopN(int topN) {return  this.stringRedisTemplate.opsForZSet().reverseRangeWithScores("W_week", 0, topN - 1);}/*** 获取月榜前topN*/@PostMapping("/getMonthTopN")public Set<ZSetOperations.TypedTuple<String>> getMonthTopN(int topN) {return  this.stringRedisTemplate.opsForZSet().reverseRangeWithScores("W_month", 0, topN - 1);}/*** 定时五秒刷新点赞次数*/public void  refreshDataHour(){while(true){long  hour=System.currentTimeMillis()/(1000*60*60);Random rand =new Random();for(int i=1;i<=26;i++){this.stringRedisTemplate.opsForZSet().incrementScore("W_hour"+hour,"player"+i,rand.nextDouble());}try{Thread.sleep(5000);}catch (InterruptedException e){e.printStackTrace();}}}/*** 定时一小时合并日榜 周榜 月榜*/public void  refreshData(){while(true) {//日榜this.refreshDay();//周榜this.refreshWeek();//月榜this.refreshMonth();try {Thread.sleep(1000 * 60 * 60);} catch (InterruptedException e) {e.printStackTrace();}}}/*** 日榜*/public void  refreshDay(){long  hour =System.currentTimeMillis()/(1000*60*60);//合并一天的keyList<String> keys=new ArrayList<>();for(int i=1;i<23;i++){String key="W_hour"+(hour-i);keys.add(key);}//求倒退23小时的并集this.stringRedisTemplate.opsForZSet().unionAndStore("W_hour"+hour,keys,"W_day");//设置当前的key  40天过期for(int i=0;i<24;i++){String key="W_hour"+(hour-i);//过期时间设置为40天stringRedisTemplate.expire(key,40, TimeUnit.DAYS);}log.info("日榜合并完成");}/*** 周榜*/public void  refreshWeek(){long hour =System.currentTimeMillis()/(1000*60*60);List<String> keys=new ArrayList<>();for(int i=1;i<24*7-1;i++){String key="W_hour"+(hour-i);keys.add(key);}this.stringRedisTemplate.opsForZSet().unionAndStore("W_hour"+hour,keys,"W_week");log.info("周刷新完成");}/*** 月榜*/public void  refreshMonth(){long hour =System.currentTimeMillis()/(1000*60*60);List<String> keys=new ArrayList<>();for(int i=1;i<24*30-1;i++){String key="W_hour"+(hour-i);keys.add(key);}this.stringRedisTemplate.opsForZSet().unionAndStore("W_hour"+hour,keys,"W_month");log.info("月刷新完成");}
}

请添加图片描述
请添加图片描述
请添加图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词