Redis最佳实践SortedSet开发用户积分实时榜单

一 背景

  • 用户玩游戏-积分实时榜单
  • IT视频热销实时榜单
  • 电商商品热销实时榜单
  • 一般的排行榜读多写少,可以对 master 进行写入操作,然后多个 slave 进行读取操作。
  • 如果是对象记得重写HashCode与Equals方法

二 数据准备

/**
 * 积分对象
 * @author cv大魔王
 * @version 1.0
 * @date 2021/5/21 15:37
 */
@Data
public class UserPointVO implements Serializable {

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

    private String username;

    private String 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 Objects.equals(username, that.username) && Objects.equals(phone, that.phone);
    }

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

模拟数据

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

    BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
    operations.add(p1, 888);
    operations.add(p2, 5456);
    operations.add(p3, 589);
    operations.add(p4, 17);
    operations.add(p5, 569);
    operations.add(p6, 742);
    operations.add(p7, 333);
    operations.add(p8, 267);
}

三 接口开发

 /**
     * 返回榜单信息前五
     * @return
     */
    @RequestMapping("real_rank1")
    public JsonData realRank() {
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        // 只要前五,如果返回全部则是0 -1
        Set<UserPointVO> set = operations.reverseRange(0, 4);
        return JsonData.buildSuccess(set);
    }

    /**
     * 返回榜单信息后五
     * @return
     */
    @RequestMapping("real_rank2")
    public JsonData realRank2() {
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        // 只要前五,如果返回全部则是0 -1
        Set<UserPointVO> set = operations.range(0, 4);
        return JsonData.buildSuccess(set);
    }


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

    /**
     * 给用户加积分
     * @return
     */
    @RequestMapping("uprank")
    public JsonData upRank(String phone,String name,int point) {
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVO vo = new UserPointVO(name, phone);

        operations.incrementScore(vo,point);
        return JsonData.buildSuccess();
    }



    /**
     * 查看某个用户积分
     * @return
     */
    @RequestMapping("mypoint")
    public JsonData myPoint(String phone,String name) {
        BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVO vo = new UserPointVO(name, phone);

        Double score = operations.score(vo);
        return JsonData.buildSuccess(score);
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值