hyperf框架在普通的接口查询会出现多个,有必要使用协程吗?
已开启一键协程
如下代码
public function getInfo(string $hehe, int $uid = 0): array
{
$data = [
'info' => [],
'num' => [
'all' => 0,
'col1' => 0,
'col2' => 0,
'col3' => 0,
],
];
$w = W::where('w', '=', $hehe)->first();
if (!$w) {
return $data;
}
$time = time();
if ($time >= $w->b_start_time && $time < $w->b_end_time) {
// 这里有必要协程处理吗?
$data['num']['col1'] = Col1::where('w', '=', $hehe)->count();
}
if ($time >= $w->e_start_time) {
// 这里有必要协程处理吗?
$userIds = All::where('w', '=', $hehe)->pluck('uid');
if ($userIds) {
$data['num']['all'] = \count(array_unique($userIds->toArray()));
}
}
// 这里有必要协程处理吗?
parallel([
function () use (&$data, $season) {
$col2Ids = Col2::where('w', '=', $hehe)
->where('type', '=', Col2::TYPE_1)
->pluck('uid');
if ($col2Ids) {
$data['num']['col2'] = count(array_unique($col2Ids->toArray()));
}
unset($teamUserIds);
},
function () use (&$data, $season) {
$col3Ids = Col3::where('w', '=', $hehe)
->where('type', '=', Col3::TYPE_2)
->pluck('uid');
if ($col3Ids) {
$data['num']['col3'] = count(array_unique($col3Ids->toArray()));
}
unset($col3Ids);
}
]);
$data['info'] = $w->toArray();
return $data;
}
return $data 后,就直接 response 回去了
请问各位大佬们,代码中标注的地方有必要加协程处理吗?能说下具体的原因就更好了
推荐文章: