Laravel 5.4通过异步队列保存模型 [英] Laravel 5.4 saving models via async queue

查看:106
本文介绍了Laravel 5.4通过异步队列保存模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图优化我的网站,并在每次加载和退出页面时都保存一个指标(页面停留时间,IP地址等)进行分析.但是,这些是我服务器上相当大的瓶颈.在查看运行时间时,我整个功能的运行大约需要1-2ms,然后保存到数据库大约需要100-200ms.因此,我的目标是运行我的函数,然后调度新任务,这将完成指标的实际保存.这样,我所有模型的保存都可以卸载到队列中.以下是我的工作副本

So I am trying to optimize my site and on every page load and exit I save a metric (time on page, ip address etc) for analytics. However these are decent sized bottlenecks on my server. When viewing the time it takes for things to run my entire function takes ~1-2ms and then saving to the DB takes ~100-200ms. So my goal is to run my function and then dispatch a new job, that will do the actual saving of the metric. This way all of the saving of my models can be offloaded to a queue. Below is a copy of my job

class SaveMetric implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Metrics $metric)
    {
        //
        $metric->save();
    }
}

然后在获取所有需要的值后在控制器功能中运行

Then in my controller function after I grab all the values I need I run this

dispatch(new SaveMetric($newMetric));

这似乎正在运行,但似乎无能为力.我想念什么吗? (编辑)这确实完成了某事〜它只是将一条记录保存到DB中,所有字段中都为null,就像我创建了一个没有任何值的新指标一样.

This seems to run but does not seem to do anything. Am I missing something? (Edit) This does ~something~ it just saves a record to the DB with null in all the fields, as if I created a new metric without any values.

  • 是否需要将队列传递到作业分派中?
  • 我是否需要运行守护程序或类似的东西来实际处理队列中的内容?

我使用工匠的make:job命令创建了工作

I created the job using the artisan make:job command

推荐答案

您非常接近.

class SaveMetric implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $metric;

    /**
     * Create a new job instance.
     *
     * @param Metrics $metric
     */
    public function __construct(Metrics $metric)
    {
        $this->metric = $metric;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->metric->save();
    }
}

根据文档:

在此示例中,请注意,我们能够将Eloquent模型直接传递到排队作业的构造函数中.由于作业正在使用SerializesModels特性,因此在处理作业时,雄辩的模型将正常地进行序列化和反序列化.如果您排队的作业在其构造函数中接受Eloquent模型,则仅该模型的标识符将被序列化到队列中.实际处理完作业后,队列系统将自动从数据库中重新检索完整的模型实例.

In this example, note that we were able to pass an Eloquent model directly into the queued job's constructor. Because of the SerializesModels trait that the job is using, Eloquent models will be gracefully serialized and unserialized when the job is processing. If your queued job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the queue system will automatically re-retrieve the full model instance from the database.

这篇关于Laravel 5.4通过异步队列保存模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆