123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace app\common\controller;
- use Redis;
- class RedisLeaderboard
- {
-
- private $redis;
-
- private $leaderboard;
-
- public function __construct($leaderboard = '') {
- $this->redis = new \Redis();
- $this->redis->connect('127.0.0.1');
- $this->leaderboard = $leaderboard;
- }
-
- public function getLeaderboard()
- {
- return $this->leaderboard;
- }
-
- public function addLeaderboard($node, $count = 1)
- {
- return $this->redis->zAdd($this->leaderboard, $count, $node);
- }
-
- public function getLeadboard($number, $asc = true, $withscores = false,$callback = null)
- {
- if ($asc) {
- $nowLeadboard = $this->redis->zRevRange($this->leaderboard, 0, $number -1, $withscores);
- } else {
- $nowLeadboard = $this->redis->zRange($this->leaderboard, 0, $number -1, $withscores);
- }
- if ($callback) {
-
- return $callback($nowLeadboard);
- } else {
- return $nowLeadboard;
- }
- }
-
- public function getNodeRank($node, $asc = true)
- {
- if ($asc) {
-
- return $this->redis->zRevRank($this->leaderboard, $node);
- } else {
- return $this->redis->zRank($this->leaderboard, $node);
- }
- }
-
- public function getZunionstore() {
- $this->redis->zUnionStore();
- }
- }
|