Created
January 1, 2011 22:45
-
-
Save czarneckid/762065 to your computer and use it in GitHub Desktop.
Creating high score leaderboards with Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NOTE: You will need at least Redis 2.1.6 to use the ZREVRANGEBYSCORE method. | |
Add players to HIGHSCORES table: | |
fossil:~ dczarnecki$ redis-cli | |
redis> zadd HIGHSCORES 1 player_1 | |
(integer) 1 | |
redis> zadd HIGHSCORES 2 player_2 | |
(integer) 1 | |
redis> zadd HIGHSCORES 3 player_3 | |
(integer) 1 | |
redis> zadd HIGHSCORES 4 player_4 | |
(integer) 1 | |
redis> zadd HIGHSCORES 4 player_5 | |
(integer) 1 | |
redis> zadd HIGHSCORES 6 player_6 | |
(integer) 1 | |
redis> zadd HIGHSCORES 7 player_7 | |
(integer) 1 | |
redis> zadd HIGHSCORES 8 player_8 | |
(integer) 1 | |
redis> zadd HIGHSCORES 9 player_9 | |
(integer) 1 | |
redis> zadd HIGHSCORES 10 player_10 | |
(integer) 1 | |
Find out how many players are in the HIGHSCORES table: | |
redis> zcard HIGHSCORES | |
(integer) 10 | |
Print out all the players that are in the HIGHSCORES table: | |
redis> zrangebyscore HIGHSCORES -inf +inf WITHSCORES | |
1. "player_1" | |
2. "1" | |
3. "player_2" | |
4. "2" | |
5. "player_3" | |
6. "3" | |
7. "player_4" | |
8. "4" | |
9. "player_5" | |
10. "4" | |
11. "player_6" | |
12. "6" | |
13. "player_7" | |
14. "7" | |
15. "player_8" | |
16. "8" | |
17. "player_9" | |
18. "9" | |
19. "player_10" | |
20. "10" | |
Print out the 1st page of the HIGHSCORES table: | |
redis> zrangebyscore HIGHSCORES -inf +inf WITHSCORES LIMIT 0 5 | |
1) "player_1" | |
2) "1" | |
3) "player_2" | |
4) "2" | |
5) "player_3" | |
6) "3" | |
7) "player_4" | |
8) "4" | |
9) "player_5" | |
10) "4" | |
Print out the 2nd page of the HIGHSCORES table: | |
redis> zrangebyscore HIGHSCORES -inf +inf WITHSCORES LIMIT 5 -1 | |
1) "player_6" | |
2) "6" | |
3) "player_7" | |
4) "7" | |
5) "player_8" | |
6) "8" | |
7) "player_9" | |
8) "9" | |
9) "player_10" | |
10) "10" | |
Print out all the players that are in the HIGHSCORES table from highest score to lowest score: | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES | |
1) "player_10" | |
2) "10" | |
3) "player_9" | |
4) "9" | |
5) "player_8" | |
6) "8" | |
7) "player_7" | |
8) "7" | |
9) "player_6" | |
10) "6" | |
11) "player_5" | |
12) "4" | |
13) "player_4" | |
14) "4" | |
15) "player_3" | |
16) "3" | |
17) "player_2" | |
18) "2" | |
19) "player_1" | |
20) "1" | |
Print out the 1st page of players that are in the HIGHSCORES table from highest score to lowest score: | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 0 5 | |
1) "player_10" | |
2) "10" | |
3) "player_9" | |
4) "9" | |
5) "player_8" | |
6) "8" | |
7) "player_7" | |
8) "7" | |
9) "player_6" | |
10) "6" | |
Print out the 2nd page of players that are in the HIGHSCORES table from highest score to lowest score: | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 5 -1 | |
1) "player_5" | |
2) "4" | |
3) "player_4" | |
4) "4" | |
5) "player_3" | |
6) "3" | |
7) "player_2" | |
8) "2" | |
9) "player_1" | |
10) "1" | |
Create an "Around Me" leaderboard with scores of individuals above and below me: | |
Find rank in reverse order: | |
redis> zrevrank HIGHSCORES player_6 | |
(integer) 5 | |
Get players above a given player: | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 2 3 | |
1) "player_9" | |
2) "9" | |
3) "player_8" | |
4) "8" | |
5) "player_7" | |
6) "7" | |
Get players below a given player: | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 6 3 | |
1) "player_5" | |
2) "4" | |
3) "player_4" | |
4) "4" | |
5) "player_3" | |
6) "3" | |
Another "Around Me" leaderboard: | |
redis> zrevrank HIGHSCORES player_4 | |
(integer) 7 | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 4 3 | |
1) "player_7" | |
2) "7" | |
3) "player_6" | |
4) "6" | |
5) "player_5" | |
6) "4" | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 8 3 | |
1) "player_3" | |
2) "3" | |
3) "player_2" | |
4) "2" | |
5) "player_1" | |
6) "1" | |
Actually, let's simplify the "Around Me" leaderboard to just be 2 calls and include the individual in the leaderboard results: | |
redis> zrevrank HIGHSCORES player_6 | |
(integer) 4 | |
redis> zrevrangebyscore HIGHSCORES +inf -inf WITHSCORES LIMIT 1 7 | |
1. "player_9" | |
2. "9" | |
3. "player_8" | |
4. "8" | |
5. "player_7" | |
6. "7" | |
7. "player_6" | |
8. "6" | |
9. "player_5" | |
10. "4" | |
11. "player_4" | |
12. "4" | |
13. "player_3" | |
14. "3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creating high score tables (leaderboards) using Redis, http://blog.agoragames.com/2011/01/01/creating-high-score-tables-leaderboards-using-redis/