-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRanking.php
145 lines (129 loc) · 3.34 KB
/
Ranking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace xutl\ranking;
use yii\base\Component;
use yii\base\InvalidConfigException;
use Carbon\Carbon;
/**
* Class Ranking
* @package xutl\ranks
*/
class Ranking extends Component
{
/**
* @var string 榜单名称
*/
public $prefix;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty ($this->prefix)) {
throw new InvalidConfigException ('The "prefix" property must be set.');
}
$this->prefix = $this->prefix . ':';
}
/**
* 添加分数
* @param string|int $identity
* @param int $scores
* @return mixed
*/
public function addScores($identity, $scores)
{
$key = $this->prefix . date('Ymd');
return $this->redis->zincrby($key, $scores, $identity);
}
/**
* 获取昨日TOP10
* @return mixed
*/
public function getYesterdayTop10()
{
$date = Carbon::now()->subDays(1)->format('Ymd');
return $this->getOneDayRankings($date, 0, 9);
}
/**
* 获取当前月份Top 10
* @return mixed
*/
public function getCurrentMonthTop10()
{
$dates = static::getCurrentMonthDates();
return $this->getMultiDaysRankings($dates, 'rank:current_month', 0, 9);
}
/**
* 获取本周Top 10
* @return mixed
*/
public function getCurrentWeekTop10()
{
$dates = static::getCurrentWeekDates();
return $this->getMultiDaysRankings($dates, 'rank:current_week', 0, 9);
}
/**
* 获得指定日期的排名
* @param string $date 20170101
* @param int $start 开始行
* @param int $stop 结束行
* @return array
*/
protected function getOneDayRankings($date, $start, $stop)
{
$key = $this->prefix . $date;
return $this->redis->zrevrange($key, $start, $stop, ['withscores' => true]);
}
/**
* 获得多天排名
* @param array $dates ['20170101','20170102']
* @param string $outKey 输出Key
* @param int $start 开始行
* @param int $stop 结束行
* @return mixed
*/
protected function getMultiDaysRankings($dates, $outKey, $start, $stop)
{
$keys = array_map(function ($date) {
return $this->prefix . $date;
}, $dates);
$weights = array_fill(0, count($keys), 1);
$this->redis->zunionstore($outKey, $keys, $weights);
return $this->redis->zrevrange($outKey, $start, $stop, ['withscores' => true]);
}
/**
* 获取本周日期
* @return array
*/
public static function getCurrentWeekDates()
{
$dt = Carbon::now();
$dt->startOfWeek();
$dates = [];
for ($day = 1; $day <= 7; $day++) {
$dates[] = $dt->format('Ymd');
$dt->addDay();
}
return $dates;
}
/**
* 获取当前月份日期
* @return array
*/
public static function getCurrentMonthDates()
{
$dt = Carbon::now();
$days = $dt->daysInMonth;
$dates = [];
for ($day = 1; $day <= $days; $day++) {
$dt->day = $day;
$dates[] = $dt->format('Ymd');
}
return $dates;
}
}