Last active
January 10, 2022 03:42
-
-
Save naveed125/7f02cdcd932908053ef10736976536d3 to your computer and use it in GitHub Desktop.
A simple leaderboard implementation using redis and PHP
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
<?php | |
// uses Predis https://github.com/nrk/predis | |
$client = new Predis\Client(); | |
// connect to the local redis server | |
$client->connect(); | |
// delete any existing key | |
$key = 'leaderboard'; | |
$client->del([$key]); | |
// create a random leaderboard | |
for($i=0; $i<20; $i++) | |
{ | |
$ret = $client->zadd($key, ["player.{$i}" => mt_rand(1, 100)]); | |
} | |
// print the top 10 | |
$ret = $client->zrevrange($key, 0, 10, ['withscores' => true]); | |
print_r($ret); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment