public
Last active

Creating high score leaderboards with Redis

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.