Laravel通知立面队列响应时间 [英] Laravel Notification Facade Queue Response Time
问题描述
Laravel文档解释说,在分发/发送通知时,您可能希望将它们排队以加快应用程序响应时间.
The Laravel docs explain that when dispatching/sending notifcations, you may want to queue them to speed up application response time.
https://laravel.com/docs/5.5/notifications#排队通知
https://laravel.com/docs/5.5/notifications#queueing-notifications
这正是我想要做的,但是我使用通知外观而不是可通知的特征来称呼它.我担心的是前者绕过队列,我需要它立即通知一组用户.
This is exactly what I want to do, however I am calling it using the notification facade rather than the notifiable trait. My concern is the former is bypassing the queue, and I need it to notify a group of users at once.
如文档中所述:
或者,您可以通过通知"外观发送通知.此功能主要在您需要向以下设备发送通知时有用多个可通知实体,例如用户集合.
Alternatively, you may send notifications via the Notification facade. This is useful primarily when you need to send a notification to multiple notifiable entities such as a collection of users.
但是当我通过立面调用我的通知时,它不会排队.我之所以知道这一点,是因为当我监视网络请求并注释掉外观调用时,我的请求从2秒(带有通知调用)变为不到0.5秒(当我将其注释掉).
But when I call my Notification via the facade, it doesn't queue. I know this because when I monitor my network requests and comment out the facade call, my request goes from over 2 seconds (with the notification call) to under .5 seconds (when I comment it out).
这是使用队列( NewAsset
)的通知类的开始:
Here is the start of my notification class using the queue (NewAsset
):
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NewAsset extends Notification implements ShouldQueue
{
use Queueable;
这是电话:
$asset = new Asset;
$asset->user_id = Auth::user()->id;
$asset->type = "Text";
$asset->content = $content;
$asset->forum_id = 1;
$asset->save();
$users = User::where("id","!=",Auth::user()->id)->get();
Notification::send($users, new NewAsset($asset,Auth::user()));
//if i comment out the notification call above, response time decreases dramatically
return;
我在做什么错了?
哦...似乎正在触发队列:
Oh... it seems it is triggering the queue:
php artisan queue:listen
[2018-03-31 15:48:22] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:22] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:23] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:23] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:24] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:24] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:25] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:25] Processed: App\Notifications\NewAsset
那为什么这么慢?:(
推荐答案
Notification :: send
速度较慢(> 2秒),机会是,您有成千上万个可通知实体推向数据库队列,这很慢,因为数千个插入语句在数据库上执行.要改善,您可以:
The Notification::send
is slow (> 2 seconds), the chance is, you have thousands of notifiable entities pushing to the database queue, which is slow because thousands of insertion statement executes on database. To improve, you can:
-
使用其他队列驱动程序,例如Amazon SQS,Beanstalkd,Redis等.它们针对低延迟的工作队列进行了优化.与数据库作为工作队列相比,它们闪电般地快.
Use other queue drivers, such as Amazon SQS, Beanstalkd, Redis etc.. They are optimized for work queue with low latency. They are lightning fast, when compared to database as a work queue.
创建另一个作业,然后让工作人员将所有通知排队,例如:
Create another job and let the worker queue all the notifications, for example:
php artisan make:job QueueUserNotificationsJob
YourController.php
dispatch(new QueueUserNotificationsJob(Auth::user()->id));
QueueUserNotificationsJob.php
public $authUserId = null;
public function __construct($authUserId) {
$this->authUserId = $authUserId;
}
public function handle() {
$users = User::where("id", "!=", $this->authUserId)->get();
Notification::send($users, new NewAsset($asset, $this->authUserId));
}
这篇关于Laravel通知立面队列响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!