A tutorial on using Redis to achieve the user points list _redis

Source: Internet
Author: User
Tags redis

The list function is a very common requirement. It is a good and quick choice to use the features of ordered sets in Redis to achieve the rankings.

The general rankings are effective, such as "User standings." If the results are not always ranked according to the overall list, it is possible that the top is always a few old users, for new users, it is really frustrating.

First of all, to a "Today's standings" bar, the collation is today users add more points from more to less.

Then the user increases the integral, all operates the record day integral increment order collection.
Assuming today is April 01, 2015, the user with a UID of 1 adds 5 points for an operation.
The Redis command is as follows:

Zincrby rank:20150401 5 1

Suppose there are several other users who also add points:

Zincrby rank:20150401 1 2
Zincrby rank:20150401 10 3

Look at the data in the ordered set rank:20150401 now (withscores parameters can be shipped with score of the element):

Zrange rank:20150401 0-1 withscores

1 "2"
2) "1"
3) "1" 4
) "5"
5) "
3" 6 "10"

To obtain TOP10 from high to low scores:

Zrevrange rank:20150401 0 9 withscores

1) "3" 2
) "Ten"
3) "1"
4) "5"
5) "
2" 6 "1"

Because there are only three elements, the data is queried.

If you record that day's score list, then the other list is simple.
Like "Yesterday's standings":

Zrevrange rank:20150331 0 9 Withscores

Achieve the "Last week's standings" by using the combined set to realize the sum of many days ' points:

Zunionstore Rank:last_week 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 an ordered set of Rank:last_week. Weight factor WEIGHTS If not, the default is 1. In order not to hide the details, deliberately write.
So the message for the TOP10 last week's standings is:

Zrevrange Rank:last_week 0 9 Withscores

"Monthly List", "Quarterly", "annual list" and so on and so on.

Here is a simple implementation of a PHP version. Using Redis relies on the PHP extension Phpredis, the code also relies on the Carbon library for processing time. The amount of code is small, so you don't have to comment.

<?php

namespace Blog\redis;

Use \redis;
Use Carbon\carbon;


Class Ranks {

  Const PREFIX = ' rank: ';

  protected $redis = null;


  Public function __construct (Redis $redis) {
    $this->redis = $redis;
  }


  The Public Function addscores ($member, $scores) {
    $key = self::P refix. Date (' Ymd ');
    return $this->redis->zincrby ($key, $scores, $member);


  protected function Getonedayrankings ($date, $start, $stop) {
    $key = self::P refix. $date;
    return $this->redis->zrevrange ($key, $start, $stop, true);

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.