tenancy / multi-tenant

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
https://tenancy.dev
MIT License
2.56k stars 394 forks source link

CacheServiceeProvider tenant identification for background processes #1006

Open juhasev opened 3 years ago

juhasev commented 3 years ago

Hyn/Tenant 5.6 the example uses $app['config']['driver'] to get the tenant UUID, however it is always null. Does anybody know how to detect the Tenant in CLI operations like background processes? I have some background processes that need to clear Cached entries.

public function boot()
{
    Cache::extend('redis_tenancy', function ($app) {
        if (PHP_SAPI === 'cli') {
            $uuid = $app['config']['driver'];
        }

        return Cache::repository(new RedisStore(
                $app['redis'],
                $uuid,
                $app['config']['cache.stores.redis.connection']
            ));
    });
} 
FloneJan commented 2 years ago

I used another solution to solve this problem, which you can check in the AppServiceProvider boot() and fix by adding a cache prefix

    public function boot()
    {
        $website = \Hyn\Tenancy\Facades\TenancyFacade::website();
        if ($website) {
            Cache::setPrefix(Str::slug($website->uuid, '_') . '_cache');
        }
    }
juhasev commented 2 years ago

Thanks @FloneJan I have to try this out.