Redis leaderboards do your own simple exercises

Source: Internet
Author: User

<?phpclass ranks{const PREFIX = ' Zhengban ';p rotected $redis = ';/* Initialize */public function __construct (Redis $redis) {$th Is->redis = $redis;} /* Added to leaderboard */public function addscores ($gameid, $score) {$key = self::P refix. Date (' Ymd '); return $this->redis-> Zincrby ($key, $score, $gameid);} /* Get leaderboard data for a particular day returns a one-dimensional array, key is Gameid,value is score */public function getonedayrankings ($date, $start, $end) {$key = self:: PREFIX.        $date; return $this->redis->zrevrange ($key, $start, $end, true);} /* Get leaderboard for days data */public function getmultidaysrankings ($dates, $start, $end) {$outKey = Null;foreach ($dates as $v) {$keys [] = Self::P refix.        $v;}        $weights = Array_fill (0, Count ($keys), 1);        $this->redis->zunion ($outKey, $keys, $weights);        $this->redis->zunion ($outKey, $keys); return $this->redis->zrevrange ($outKey, $start, $end, True);}} $host = "192.168.1.114"; $port = 6379; $pwd = "123456"; $redis = new Redis (); if ($redis->connect ($host, $port) = = False) { Exit (' {' REsult ":"-1 "} ');   Connection failed}/* auth password */if ($redis->auth ($pwd) = = False) {exit (' {' result ': '-2 '} '); Authentication failed} $Ranks = new ranks ($redis);//$Ranks->addscores (12,1); er = $Ranks->getmultidaysrankings (Array ( 20151021,20151022,20151020), 0,9); Var_dump (er);

Redis leaderboards do your own simple exercises

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.