当前位置: 首页 > news >正文

SortedSet结构之用户积分实时榜单实战

Redis 中的SortedSet结构非常适合用于实现实时榜单的场景,它根据成员的分数自动进行排序,支持高效的添加、更新和查询操作。

SortedSet实时榜单的一些典型应用场景:

游戏中的玩家排行榜:在多人在线游戏中,使用 SortedSet来维护玩家的得分排行榜,可以按照玩家的得分来排序,方便展示顶级玩家或者好友间的排名情况。

电商热销榜:像淘宝、京东等电商平台的热销商品榜单(例如热销手机、电脑等)。通过 SortedSet可以轻松维护基于销量或其他指标的商品排名,并能快速获取最新的排名信息。

体育赛事积分榜:在体育赛事的应用场景中,利用 SortedSet维护各队伍或运动员的比赛积分、胜率等统计数据的排行榜,以便实时更新和展示最新的排名情况。

下面我们来实战一下用户积分实时榜单!今天实战的内容有查看全部榜单(从大到小);查看前三名的榜单(从大到小);查看某个用户的排名;给某个用户加积分,之后返回所有榜单;查看个人的积分。

对象准备(记得重写HashCode与Equals方法):

public class UserPointVO {
    private String username;
    private String phone;
    public UserPointVO(String username, String phone) {
        this.username = username;
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserPointVO that = (UserPointVO) o;
        return phone.equals(that.phone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(phone);
    }
}

数据准备:

@Test
	void testData() {

		UserPointVO p1 = new UserPointVO("老A","324");
		UserPointVO p2 = new UserPointVO("老B","242");
		UserPointVO p3 = new UserPointVO("老C","542345");
		UserPointVO p4 = new UserPointVO("老D","235");
		UserPointVO p5 = new UserPointVO("老E","1245");
		UserPointVO p6 = new UserPointVO("老F","2356432");
		UserPointVO p7 = new UserPointVO("老G","532332");
		UserPointVO p8 = new UserPointVO("老H","13113");

		BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");

		operations.add(p1,324);
		operations.add(p2,542);
		operations.add(p3,52);
		operations.add(p4,434);
		operations.add(p5,1123);
		operations.add(p6,64);
		operations.add(p7,765);
		operations.add(p8,8);

	}

redis中的数据:

 

我们首先开发返回全部榜单,从大到小的接口:

/**
     *返回全部榜单,从大到小
     * @return
     */
    @RequestMapping("real_rank1")
    public JsonData realRank1(){
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        Set<UserPointVO> set = operations.reverseRange(0, -1);
        return JsonData.buildSuccess(set);
    }

打印结果为:

 

 接着开发返回前三名榜单的接口:

 /**
     *返回指定大小榜单,从大到小,这里是返回前三名
     * @return
     */
    @RequestMapping("real_rank2")
    public JsonData realRank2(){
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        Set<UserPointVO> set = operations.reverseRange(0, 2);
        return JsonData.buildSuccess(set);
    }

打印结果为:

 

查看某个用户的排名:

/**
     * 查看某个用户的排名
     * @param name
     * @param phone
     * @return
     */
    @RequestMapping("find_myrank")
    public JsonData realMyRank(String name,String phone){
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVO userPointVO = new UserPointVO(name,phone);
        long rank = operations.reverseRank(userPointVO);
        return JsonData.buildSuccess(++rank);
    }

这里我们查看老H的排名,打印结果为:

 

给某个用户加积分:

/**
     * 给某个用户加积分,之后返回所有榜单
     * @param name
     * @param phone
     * @param point
     * @return
     */
    @RequestMapping("uprank")
    public JsonData upRank(String name,String phone,int point){
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVO userPointVO = new UserPointVO(name,phone);
        operations.incrementScore(userPointVO,point);

        Set<UserPointVO> set = operations.reverseRange(0, -1);
        return JsonData.buildSuccess(set);
    }

这里我们给老H加1000的积分,打印结果为:

 

查看个人的积分:

/**
     * 查看个人的积分
     * @param name
     * @param phone
     * @return
     */
    @RequestMapping("mypoint")
    public JsonData mypoint(String name,String phone){
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVO userPointVO = new UserPointVO(name,phone);
        double score = operations.score(userPointVO);
        return JsonData.buildSuccess(score);
    }

这里我们还是查看老H的积分:

 

 

 

 

 

 

 

 

    http://www.dtcms.com/a/113183.html

    相关文章:

  1. WordPress图标设置插件,免费功能小巧
  2. 武装自己的Kali
  3. 轨道交通装备三维检测与轻量化设计
  4. Cookie、Session、Token、JWT的区别和使用场景
  5. 深度测评 | 聚铭下一代智慧安全运营中心如何破解电力行业安全运维难题?
  6. C++ 判断字符是否为数字或字母:isalpha、isdigit 和 isalnum 函数详解
  7. 【2-8】同步通信与异步通信
  8. 数据库性能优化(sql优化)_子查询02_yxy
  9. 二十种中药果实识别分类系统,Python/resnet18/pytorch
  10. C++_类和对象(下)
  11. 无状态版的DHCPv6是不是SLAAC? 笔记250405
  12. 【LeetCode Solutions】LeetCode 146 ~ 150 题解
  13. JVM深入原理(六)(二):双亲委派机制
  14. 元宇宙概念下,UI 设计如何打造沉浸式体验?
  15. 从零开始玩python--python版植物大战僵尸来袭
  16. 数字化转型中的开源AI智能客服与S2B2C商城小程序的融合创新
  17. ☕️ 关于本博客 ☀️
  18. OSCP - Proving Grounds- SoSimple
  19. VUE+SPRINGBOOT+语音技术实现智能语音歌曲管理系统
  20. 交换机与路由器的区别
  21. 故障矩阵像素照片效果ps标题文本特效滤镜样机 Glitched Arcade Text Logo Effect
  22. 【Python】数组的条件逻辑统计运算元素排序
  23. Java的Selenium的特殊元素操作与定位之window切换
  24. 推荐系统的注意力进化:从 Self-Attention 到 Target-Attention
  25. BT-Basic函数之首字母T
  26. 《打破SQL与AI框架对接壁垒,解锁融合新路径》
  27. 文章记单词 | 第25篇(六级)
  28. 深度学习之微调
  29. 练习题:123
  30. 量子纠错码实战:从Shor码到表面码