redis 排行榜的独特评分 [英] Unique scoring for redis leaderboard

查看:87
本文介绍了redis 排行榜的独特评分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 redis 创建一个排行榜,并试图找出一个很好的方法来处理获得相同高分的玩家(获得相同高分的玩家应该按照谁先获得高分进行排序)

I am using redis to create a leaderboard and trying to figure out a nice way to deal with players who achieve the same high score (players with the same high score should be ordered by who achieved that high score first)

我曾想过做以下...

zadd leaderboard highscore.timestamp player_id.attempt_number

但是,当我执行 zrevrange 时,时间戳显然会以错误的顺序排列.

However, when I do zrevrange the timestamps are obviously going to be in the incorrect order.

示例输入:

zadd leaderboard 20.123 5.1
zadd leaderboard 20.125 5.2
zadd leaderboard 20.012 5.3
zadd leaderboard 21.024 5.4

输出:

zrevrange leaderboard 0 -1
5.4
5.2
5.1
5.3

所需的输出:

5.4
5.3
5.1
5.2

推荐答案

一种选择是做 zadd 排行榜 highscore.(Long.MAX_VALUE - timestamp) player_id.attempt_number(将 Long.MAX_VALUE 更改为无论您的时间戳的合理最高值是什么 - 一个有符号的 64 位 int 将存储大约 3 亿年的毫秒数,因此可能不需要)

One option would be to do zadd leaderboard highscore.(Long.MAX_VALUE - timestamp) player_id.attempt_number (change Long.MAX_VALUE to whatever is a reasonable highest possible value for your timestamp - A signed 64 bit int will store about 300 million years worth of miliseconds, so that might not be needed)

不要忘记添加前导零:)

Don't forget to add leading zeroes :)

这篇关于redis 排行榜的独特评分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆