PHP 性能优化:生成器 yield 的初体验
需求:用户发贴,粉丝收到通知。
了解了一下生成器,就用一下,虽然理解的还不是很深刻。
<?php
namespace App\Jobs;
use App\Models\User;
use App\Models\UserFollow;
use App\Notifications\NewThread;
class NewThreadNotification extends Job {
/** @var mixed */
private $thread;
private $actor;
public function __construct($thread,$actor) {
$this->thread = $thread;
$this->actor = $actor;
}
public function handle()
{
app('log')->info('----粉丝通知--这里是发送通知队列-----');
//获取这个用户的所有粉丝
$follow = UserFollow::query()
->where('to_user_id',$this->thread->user_id)
->with('fromUser')
->get();
$users = $this->getFollowUser($follow);
foreach ($users as $v){
//发送微信通知
$v->fromUser->notify(new NewThread($this->actor,$this->thread));
}
app('log')->info('----粉丝通知--发送完成-----');
$this->delete();
}
public function getFollowUser($follow)
{
foreach ($follow as $v){
yield $v;
}
}
}
还在考虑,要不要分组,目前粉丝量顶天几万。以上是初体验。。。
分片处理:
laravel cursor()处理:
cursor()源码也看了一下,我猜laravel里面的懒加载应该是这个原理;
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: