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. Redis ordered collections are ideal for storing ordered, non-repeatable data
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:(zadd update)
ZINCRBY rank:20150401 5 1
Let's say that several other users have added points as well:
ZINCRBY rank:20150401 1 2
ZINCRBY rank:20150401 10 3
Take a look at the data in the Ordered collection rank:20150401 (the Withscores parameter can be accompanied by a score to get the element):(Zrange get top users)
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:
rank:last_week
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.
Reference from http://segmentfault.com/a/1190000002694239
Redis ordered aggregation for leaderboard functions