如何在Laravel 5中使用redis为多租户多数据库架构应用程序执行排队和缓存?

I am working on a multi-tenant multi-database architecture application using Laravel which basically means that every tenant in the application has their own database, own sets of users, resources and so on.

Now I am trying to implement queues and caching in the application, then I am trying to use redis for that. A sample code looks like this:

$mailer->send('emails.welcome', ['user' => $user], function ($message) use ($user) {
    $message->from("admin@admin.com", "Admin");
    $message->to($user->email, $user->first_name)->subject("Welcome!");
});

This is to send a welcome email to user sign-up. But the queuing is storing all the queues in the same database in the same Redis instance, different tenant emails will get mixed up as far as I can tell.

How do I hook into Laravel 5 and change the queue behavior to either store the jobs for each tenant in a separate database or store extra meta-information about the tenant a particular job belongs to? And then also how would I tell Laravel to parse that extra meta-information and connect to the right tenant database before proceeding with the job?

2个回答



对于Queue系统的适当工作,您需要使用自己的 \ Illuminate \ Queue \ SerializesModels </ code>实现 你的工作中的特质。 Wheech将在 Job :: __ sleep()</ code>和 Job :: __ wakeup()</ code>上保存并初始化正确的数据库连接。

请查看 TenantAwareJob trait </ a > hyn / multi-tenant 包。</ p> \ n

对于Cache系统的适当工作,您需要使用取决于当前主机的前缀。 看看 hyn / multi-tenant 开发人员如何推荐实施此功能 。 </ p>
</ div>

展开原文

原文

For appropriate work of Queue system, you need to use your own implementation of \Illuminate\Queue\SerializesModels trait inside your Jobs. Wheech will save and init correct DB connection on Job::__sleep() and Job::__wakeup(). Take a look at TenantAwareJob trait of hyn/multi-tenant package.

For appropriate work of Cache system, you need to use a prefix which depends on the current host. Take a look how hyn/multi-tenant developers recommend to implement this.

This shouldn't be a problem generally as the queue is simply a queue. i.e. the tasks in it contain the real needed data, if you want to 'categorize' the queues for better management, you may the onQueue method to specify the queue or onConnection to specify the connection you want to dispatch to.

e.g.

EmailJob::dispatch($podcast)
    ->onConnection('sqs')
    ->onQueue('tenant1');

You may also create a queue for a particular connection as follows and push jobs to it:

$tenant1Connection = Queue::connection('connection_name');
$tenant1Connection->pushOn('queue_name', $job)

If you need to avoid the facade, you can do:

app('queue')->connection('connection_name')->pushOn('queue_name', $job);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问