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

时间:2021-11-25 12:47:17

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.

我正在使用Laravel开发一个多租户多数据库架构应用程序,这基本上意味着应用程序中的每个租户都有自己的数据库,自己的用户集,资源等等。

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:

现在我正在尝试在应用程序中实现队列和缓存,然后我尝试使用redis。示例代码如下所示:

$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.

这是向用户注册发送欢迎电子邮件。但排队是将同一个数据库中的所有队列存储在同一个Redis实例中,据我所知,不同的租户电子邮件会混淆。

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?

如何挂钩到Laravel 5并更改队列行为,以将每个租户的作业存储在单独的数据库中,或者存储有关特定作业所属租户的额外元信息?然后我还要告诉Laravel在继续工作之前解析额外的元信息并连接到正确的租户数据库?

2 个解决方案

#1


1  

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.

对于Queue系统的适当工作,您需要在Jobs中使用自己的\ Illuminate \ Queue \ SerializesModels特性实现。 Wheech将在Job :: __ sleep()和Job :: __ wakeup()上保存并初始化正确的数据库连接。看看hyn / multi-tenant包的TenantAwareJob特性。

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.

对于Cache系统的适当工作,您需要使用取决于当前主机的前缀。看看hyn /多租户开发人员如何推荐实现这一点。

#2


0  

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.

这通常不应该是一个问题,因为队列只是一个队列。即,其中的任务包含真正需要的数据,如果要对队列进行“分类”以便更好地进行管理,则可以使用onQueue方法指定队列,或者使用onConnection指定要分派的连接。

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);

#1


1  

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.

对于Queue系统的适当工作,您需要在Jobs中使用自己的\ Illuminate \ Queue \ SerializesModels特性实现。 Wheech将在Job :: __ sleep()和Job :: __ wakeup()上保存并初始化正确的数据库连接。看看hyn / multi-tenant包的TenantAwareJob特性。

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.

对于Cache系统的适当工作,您需要使用取决于当前主机的前缀。看看hyn /多租户开发人员如何推荐实现这一点。

#2


0  

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.

这通常不应该是一个问题,因为队列只是一个队列。即,其中的任务包含真正需要的数据,如果要对队列进行“分类”以便更好地进行管理,则可以使用onQueue方法指定队列,或者使用onConnection指定要分派的连接。

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);