java+redission排行榜实现

java+redission排行榜实现,第1张

java+redission排行榜实现 Redis 排行榜实现笔记(勿喷,高手请留言指点) 项目需求

统计每个用户 每日 每月 每年 搜索到关键词排行榜

代码实现

排行榜数据采集端

//获取今日日期
String toDay = LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
StringBuffer sb = new StringBuffer();
//构建用户今日排行榜明年空间                sb.append(RedisKeyConstant.KEYWORD_RANKING).append("day").append(":").append(toDay).append(":").append(openId);
String rankingKey = sb.toString();

RScoredSortedSet scoredSortedSet = redissonClient.getScoredSortedSet(rankingKey);
//判断搜索词kw是否存在
Double score = scoredSortedSet.getScore(kw);
if(score == null || score < 1){
	//添加搜索词
   scoredSortedSet.add(1,kw);
}else{
	//对搜索词分数加1
   scoredSortedSet.addScore(kw,1);
}
 

排行榜数据展示端

LocalDateTime now = LocalDateTime.now();
String toDay = now.format(DateTimeFormatter.BASIC_ISO_DATE);
//获取当天排行榜
String rankingToday = RedisKeyConstant.KEYWORD_RANKING+"day:"+toDay;
//获取今日排行榜
getRanking(rankingToday+":"+openId))


//获取当月排行榜
//获取当月的第几天
int day = now.getDayOfMonth();
String rankingToMonth = RedisKeyConstant.KEYWORD_RANKING+"month:"+toDay.substring(0,toDay.length()-2);
mergeRanking(day,rankingToday.substring(0,rankingToday.length()-2),rankingToMonth,openId);
getRanking(rankingToMonth+":"+openId);

//获取当年排行榜
//获取当年的第几月
int month = now.getMonthValue();
int year = now.getYear();
String rankingToYear = RedisKeyConstant.KEYWORD_RANKING+"year:"+year;
mergeRanking(month,rankingToMonth.substring(0,rankingToMonth.length()-2),rankingToYear,openId);
getRanking(rankingToYear+":"+employee.getOpenId())


//统计并计算每日每月每年的排行榜
private void mergeRanking(int day, String needRankingKey,String destinationRankingKey,String openId) {
        RScoredSortedSet sortedSetMonth = redissonClient.getScoredSortedSet(destinationRankingKey+":"+openId);
        List list = Lists.newArrayList();
        IntStream.range(0,day).forEach(it -> {
            list.add(needRankingKey + StrUtil.fill(String.valueOf(it+1),'0',2,true) +":"+openId);
        });
        sortedSetMonth.union(list.toArray(new String[]{}));
}

//获取搜索词排行榜比根据分数排序
private List getRanking(String rankingKey){
        List kws = Lists.newlinkedList();
        RScoredSortedSet scoredSortedSet = redissonClient.getScoredSortedSet(rankingKey);
        if (scoredSortedSet != null && scoredSortedSet.isExists()) {
            scoredSortedSet.entryRangeReversed(0, 2).stream().forEach(it -> {
                kws.add(BussOppoKeyWord.build(String.valueOf(it.getValue()),it.getScore().longValue()));
            });
        }
        if(!CollUtil.isEmpty(kws)){
            IntStream.range(0,kws.size()).forEach(it -> {
                BussOppoKeyWord keyWord = kws.get(it);
                if(it < kws.size()-1){
                    if(keyWord.getTotal() == kws.get(it+1).getTotal()){
                        if(keyWord.getKeyWord().compareTo(kws.get(it + 1).getKeyWord()) > 1){
                            Collections.swap(kws,it,it+1);
                        }
                    }
                }
            });
        }
        return kws;
    }
 
效果图 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5612726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)