Redis ordered aggregation for leaderboard functions

Source: Internet
Author: User

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_weekrank: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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.