mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 01:44:04 +00:00
prefix cache bootstrapper and tests
This commit is contained in:
parent
898d6c5d3b
commit
d6da626f73
4 changed files with 32 additions and 40 deletions
|
|
@ -102,6 +102,7 @@ return [
|
||||||
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
||||||
|
// Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper::class, // prefix cache keys
|
||||||
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,38 +4,30 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers;
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
use Illuminate\Cache\CacheManager;
|
|
||||||
use Illuminate\Cache\Repository;
|
use Illuminate\Cache\Repository;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Stancl\Tenancy\CacheManager as TenantCacheManager;
|
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
protected ?string $originalPrefix;
|
protected null|string $originalPrefix = null;
|
||||||
|
protected string $storeName;
|
||||||
public function __construct(
|
|
||||||
protected Application $app,
|
|
||||||
protected ?string $storeName = null,
|
|
||||||
protected ?string $cacheKeyBase = null,
|
|
||||||
) {
|
|
||||||
$this->originalPrefix = config('cache.prefix');
|
|
||||||
|
|
||||||
$this->storeName ??= config('cache.default');
|
|
||||||
|
|
||||||
$this->cacheKeyBase ??= 'tenant_id_';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->setCachePrefix($this->cacheKeyBase . $tenant->id);
|
$this->originalPrefix = config('cache.prefix');
|
||||||
|
$this->storeName = config('cache.default');
|
||||||
|
|
||||||
|
$this->setCachePrefix('tenant_id_' . $tenant->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
{
|
{
|
||||||
$this->setCachePrefix($this->originalPrefix);
|
if ($this->originalPrefix) {
|
||||||
|
$this->setCachePrefix($this->originalPrefix);
|
||||||
|
$this->originalPrefix = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setCachePrefix(string $prefix): void
|
protected function setCachePrefix(string $prefix): void
|
||||||
|
|
@ -44,21 +36,17 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
app('cache')->forgetDriver($this->storeName);
|
app('cache')->forgetDriver($this->storeName);
|
||||||
|
|
||||||
// cache()->purge();
|
// This is important because the `CacheManager` will have the `$app['config']` array cached
|
||||||
//
|
// with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes
|
||||||
// app('cache')->forgetDriver($this->storeName);
|
// the `$store` but doesn't update the `$app['config']`.
|
||||||
//
|
app()->forgetInstance('cache');
|
||||||
// // This is important because the `CacheManager` will have the `$app['config']` array cached
|
|
||||||
// // with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes
|
//This is important because the Cache Repository is using an old version of the CacheManager
|
||||||
// // the `$store` but doesn't update the `$app['config']`.
|
app()->forgetInstance('cache.store');
|
||||||
// app()->forgetInstance('cache');
|
|
||||||
//
|
// Forget the cache repository in the container
|
||||||
// //This is important because the Cache Repository is using an old version of the CacheManager
|
app()->forgetInstance(Repository::class);
|
||||||
// app()->forgetInstance('cache.store');
|
|
||||||
//
|
Cache::clearResolvedInstances();
|
||||||
// // Forget the cache repository in the container
|
|
||||||
// app()->forgetInstance(Repository::class);
|
|
||||||
//
|
|
||||||
// Cache::clearResolvedInstances();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use Illuminate\Support\Str;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
|
@ -73,11 +74,9 @@ test('database data is separated', function () {
|
||||||
expect(DB::table('users')->first()->name)->toBe('Foo');
|
expect(DB::table('users')->first()->name)->toBe('Foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cache data is separated', function () {
|
test('cache data is separated', function (string $bootstrapper) {
|
||||||
config([
|
config([
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [$bootstrapper],
|
||||||
CacheTenancyBootstrapper::class,
|
|
||||||
],
|
|
||||||
'cache.default' => 'redis',
|
'cache.default' => 'redis',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -112,7 +111,10 @@ test('cache data is separated', function () {
|
||||||
|
|
||||||
// Asset central is still the same
|
// Asset central is still the same
|
||||||
expect(Cache::get('foo'))->toBe('central');
|
expect(Cache::get('foo'))->toBe('central');
|
||||||
});
|
})->with([
|
||||||
|
'CacheTenancyBootstrapper' => CacheTenancyBootstrapper::class,
|
||||||
|
'PrefixCacheTenancyBootstrapper' => PrefixCacheTenancyBootstrapper::class,
|
||||||
|
]);
|
||||||
|
|
||||||
test('redis data is separated', function () {
|
test('redis data is separated', function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use Dotenv\Dotenv;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use PDO;
|
use PDO;
|
||||||
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Facades\GlobalCache;
|
use Stancl\Tenancy\Facades\GlobalCache;
|
||||||
use Stancl\Tenancy\Facades\Tenancy;
|
use Stancl\Tenancy\Facades\Tenancy;
|
||||||
|
|
@ -113,6 +113,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
|
$app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
|
||||||
|
$app->singleton(PrefixCacheTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPackageProviders($app)
|
protected function getPackageProviders($app)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue