mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:44:03 +00:00
Use $addTags approach again
This commit is contained in:
parent
58e008a679
commit
8f5a4e4eb6
5 changed files with 56 additions and 45 deletions
|
|
@ -192,6 +192,7 @@ return [
|
||||||
'cache' => [
|
'cache' => [
|
||||||
'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call.
|
'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call.
|
||||||
'prefix_base' => 'tenant_', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
|
'prefix_base' => 'tenant_', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
|
||||||
|
'override_manager' => false,
|
||||||
],
|
],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers;
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
use Illuminate\Cache\CacheManager;
|
use Stancl\Tenancy\CacheManager;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
|
||||||
use Illuminate\Support\Facades\Cache;
|
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* todo name.
|
* todo name.
|
||||||
|
|
@ -17,42 +15,13 @@ use Stancl\Tenancy\Contracts\Tenant;
|
||||||
*/
|
*/
|
||||||
class CacheTagsBootstrapper implements TenancyBootstrapper
|
class CacheTagsBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
protected ?CacheManager $originalCache = null;
|
|
||||||
public static string $cacheManagerWithTags = \Stancl\Tenancy\CacheManager::class;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
protected Application $app
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->resetFacadeCache();
|
CacheManager::$addTags = true;
|
||||||
|
|
||||||
$this->originalCache ??= $this->app['cache'];
|
|
||||||
$this->app->extend('cache', function () {
|
|
||||||
return new static::$cacheManagerWithTags($this->app);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
{
|
{
|
||||||
$this->resetFacadeCache();
|
CacheManager::$addTags = false;
|
||||||
|
|
||||||
$this->app->extend('cache', function () {
|
|
||||||
return $this->originalCache;
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->originalCache = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This wouldn't be necessary, but is needed when a call to the
|
|
||||||
* facade has been made prior to bootstrapping tenancy. The
|
|
||||||
* facade has its own cache, separate from the container.
|
|
||||||
*/
|
|
||||||
public function resetFacadeCache(): void
|
|
||||||
{
|
|
||||||
Cache::clearResolvedInstances();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ use Illuminate\Cache\CacheManager as BaseCacheManager;
|
||||||
|
|
||||||
class CacheManager extends BaseCacheManager
|
class CacheManager extends BaseCacheManager
|
||||||
{
|
{
|
||||||
|
public static bool $addTags = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add tags and forward the call to the inner cache store.
|
* Add tags and forward the call to the inner cache store.
|
||||||
*
|
*
|
||||||
|
|
@ -18,6 +20,7 @@ class CacheManager extends BaseCacheManager
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
|
if (tenancy()->initialized && static::$addTags) {
|
||||||
$tags = [config('tenancy.cache.tag_base') . tenant()?->getTenantKey()];
|
$tags = [config('tenancy.cache.tag_base') . tenant()?->getTenantKey()];
|
||||||
|
|
||||||
if ($method === 'tags') {
|
if ($method === 'tags') {
|
||||||
|
|
@ -35,4 +38,7 @@ class CacheManager extends BaseCacheManager
|
||||||
|
|
||||||
return $this->store()->tags($tags)->$method(...$parameters);
|
return $this->store()->tags($tags)->$method(...$parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return parent::__call($method, $parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use Illuminate\Cache\CacheManager;
|
||||||
use Illuminate\Database\Console\Migrations\FreshCommand;
|
use Illuminate\Database\Console\Migrations\FreshCommand;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\CacheManager as TenancyCacheManager;
|
||||||
use Stancl\Tenancy\Contracts\Domain;
|
use Stancl\Tenancy\Contracts\Domain;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||||
|
|
@ -133,5 +134,12 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (! $this->app['config']['tenancy.cache.override_manager']) {
|
||||||
|
// todo https://discord.com/channels/976506366502006874/976513756576243733/1097778320692740096
|
||||||
|
$this->app->singleton('cache', function ($app) {
|
||||||
|
return new TenancyCacheManager($app);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\SpecificCacheStoreService;
|
use Stancl\Tenancy\Tests\Etc\SpecificCacheStoreService;
|
||||||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\CacheManager as TenancyCacheManager;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
|
|
@ -21,6 +22,7 @@ beforeEach(function () {
|
||||||
'cache.stores.' . $secondCacheDriver = 'redis2' => config('cache.stores.redis'),
|
'cache.stores.' . $secondCacheDriver = 'redis2' => config('cache.stores.redis'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
TenancyCacheManager::$addTags = false;
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
|
||||||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||||
|
|
||||||
|
|
@ -29,10 +31,35 @@ beforeEach(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
|
TenancyCacheManager::$addTags = false;
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Tenancy overrides CacheManager', function () {
|
||||||
|
// todo Change this to 'Tenancy overrides CacheManager only if configured to do so' after changing TenancyServiceProvider structure
|
||||||
|
// Since we override the manager in TSP by default, we can't test if the overriding is disabled by changing the override_manager config key
|
||||||
|
$tenancyCacheManager = TenancyCacheManager::class;
|
||||||
|
|
||||||
|
expect(app('cache')::class)->toBe($tenancyCacheManager);
|
||||||
|
expect(app(CacheManager::class)::class)->toBe($tenancyCacheManager);
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create(['id' => 'first']));
|
||||||
|
|
||||||
|
expect(app('cache')::class)->toBe($tenancyCacheManager);
|
||||||
|
expect(app(CacheManager::class)::class)->toBe($tenancyCacheManager);
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create(['id' => 'second']));
|
||||||
|
|
||||||
|
expect(app('cache')::class)->toBe($tenancyCacheManager);
|
||||||
|
expect(app(CacheManager::class)::class)->toBe($tenancyCacheManager);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
expect(app('cache')::class)->toBe($tenancyCacheManager);
|
||||||
|
expect(app(CacheManager::class)::class)->toBe($tenancyCacheManager);
|
||||||
|
});
|
||||||
|
|
||||||
test('correct cache prefix is used in all contexts', function () {
|
test('correct cache prefix is used in all contexts', function () {
|
||||||
$originalPrefix = config('cache.prefix');
|
$originalPrefix = config('cache.prefix');
|
||||||
$prefixBase = config('tenancy.cache.prefix_base');
|
$prefixBase = config('tenancy.cache.prefix_base');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue