The leaderboard feature is a very common requirement. Using the attributes of an ordered collection in Redis to achieve a leaderboard is a good and fast choice.
The general leaderboard is effective, such as the "User standings". If the results are not always in line with the overall list, it may be the top of a few old users, for new users, it is really frustrating.
First of all, a "Today's standings," The collation is today users add more points from the less.
Then, when users add points, they operate an ordered set of points added on the day of recording.
Assuming today is April 01, 2015, the user with the UID of 1 has added 5 points for an operation.
The Redis command is as follows:
ZINCRBY rank:20150401 5 1
Let's say that several other users have added points as well:
ZINCRBY rank:20150401 1 2ZINCRBY rank:20150401 10 3
Take a look at the data in the Ordered collection rank:20150401 (the Withscores parameter can be supplied with the score for the element):
ZRANGE rank:20150401 0 -1 withscores
1) "2"2) "1"3) "1"4) "5"5) "3"6) "10"
Get TOP10 by score from high to Low:
ZREVRANGE rank:20150401 0 9 withscores
1) "3"2) "10"3) "1"4) "5"5) "2"6) "1"
Because there are only three elements, the data is queried.
If you record the day's leaderboard every day, then the list of other tricks is simple.
such as "Yesterday's standings":
ZREVRANGE rank:20150331 0 9 withscores
Achieve "Last week's standings" by using a combination of multiple days of integration:
7 rank:20150323 rank:20150324 rank:20150325 rank:20150326 rank:20150327 rank:20150328 rank:20150329 WEIGHTS 1 1 1 1 1 1 1
This merges the 7-day integration record into the ordered set Rank:last_week. Weight factor WEIGHTS If not given, the default is 1. In order not to hide the details, deliberately written.
So the query for last week's standings TOP10 is:
ZREVRANGE rank:last_week 0 9 withscores
"Monthly Standings", "Quarterly Charts", "annual charts" and so on.
A simple implementation of the PHP version is given below. Using Redis to rely on PHP extension Phpredis, the code also relies on the Carbon library for processing time. The code is very small, so don't hit the comment.
<?phpNamespaceBlog\Redis;Use \Redis;Usecarbon\Carbon;ClassRanks {Const PREFIX =' Rank: ';Protected$redis =NullPublicfunction__construct(Redis$redis) {$this->redis =$redis; }PublicfunctionAddscores($member,$scores) {$key =Self::P refix. Date' YMD ');Return$this->redis->zincrby ($key,$scores,$member); }ProtectedfunctionGetonedayrankings($date,$start,$stop) {$key =Self::P refix.$date;Return$this->redis->zrevrange ($key,$start,$stop,true); }ProtectedfunctionGetmultidaysrankings($dates,$outKey,$start,$stop) {$keys = Array_map (function($date) {ReturnSelf::P refix.$date; },$dates);$weights = Array_fill (0, COUNT ($keys),1);$this->redis->zunion ($outKey,$keys,$weights);Return$this->redis->zrevrange ($outKey,$start,$stop,true); }PublicfunctionGetYesterdayTop10() {$date = Carbon::now ()->subdays (1)->format (' YMD ');Return$this->getonedayrankings ($date,0,9); }PublicStaticfunctionGetcurrentmonthdates() {$dt = Carbon::now ();$days =$dt->daysinmonth;$dates =Array ();for ($day =1;$day <= $days; $day + +) { $dt->day = $day; $dates [] = $dt->format ( YMD ‘); } return $dates;} public function getcurrentmonthtop10 () { $dates = self::getcurrentmonthdates (); return $this->getmultidaysrankings ( $dates, ' Rank:current_month ', 0, 9); }}
Using Redis to achieve leaderboard functionality