Skip to content

Instantly share code, notes, and snippets.

@naveed125
Last active January 10, 2022 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveed125/7f02cdcd932908053ef10736976536d3 to your computer and use it in GitHub Desktop.
Save naveed125/7f02cdcd932908053ef10736976536d3 to your computer and use it in GitHub Desktop.
A simple leaderboard implementation using redis and PHP
<?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