mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
Override cache manager only in CacheTagsBootstrapper
This commit is contained in:
parent
1d52096d6e
commit
2171ca9c68
7 changed files with 45 additions and 58 deletions
|
|
@ -190,7 +190,6 @@ return [
|
|||
* You can clear cache selectively by specifying the tag.
|
||||
*/
|
||||
'cache' => [
|
||||
'manager' => CacheManager::class,
|
||||
'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.
|
||||
],
|
||||
|
|
|
|||
|
|
@ -4,24 +4,50 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Stancl\Tenancy\CacheManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
|
||||
/**
|
||||
* todo name.
|
||||
*
|
||||
* Separate tenant cache using tagging.
|
||||
*/
|
||||
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
|
||||
{
|
||||
CacheManager::$addTags = true;
|
||||
$this->resetFacadeCache();
|
||||
|
||||
$this->originalCache ??= $this->app['cache'];
|
||||
$this->app->extend('cache', function () {
|
||||
return new static::$cacheManagerWithTags($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
CacheManager::$addTags = false;
|
||||
$this->resetFacadeCache();
|
||||
|
||||
$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,8 +10,6 @@ use Illuminate\Cache\CacheManager as BaseCacheManager;
|
|||
|
||||
class CacheManager extends BaseCacheManager
|
||||
{
|
||||
public static bool $addTags = false;
|
||||
|
||||
/**
|
||||
* Add tags and forward the call to the inner cache store.
|
||||
*
|
||||
|
|
@ -20,25 +18,21 @@ class CacheManager extends BaseCacheManager
|
|||
*/
|
||||
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') {
|
||||
$count = count($parameters);
|
||||
if ($method === 'tags') {
|
||||
$count = count($parameters);
|
||||
|
||||
if ($count !== 1) {
|
||||
throw new \Exception("Method tags() takes exactly 1 argument. $count passed.");
|
||||
}
|
||||
|
||||
$names = $parameters[0];
|
||||
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/9.x/cache#removing-tagged-cache-items
|
||||
|
||||
return $this->store()->tags(array_merge($tags, $names));
|
||||
if ($count !== 1) {
|
||||
throw new \Exception("Method tags() takes exactly 1 argument. $count passed.");
|
||||
}
|
||||
|
||||
return $this->store()->tags($tags)->$method(...$parameters);
|
||||
$names = $parameters[0];
|
||||
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/9.x/cache#removing-tagged-cache-items
|
||||
|
||||
return $this->store()->tags(array_merge($tags, $names));
|
||||
}
|
||||
|
||||
return parent::__call($method, $parameters);
|
||||
return $this->store()->tags($tags)->$method(...$parameters);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,10 +133,5 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
|
||||
return $instance;
|
||||
});
|
||||
|
||||
// todo https://discord.com/channels/976506366502006874/976513756576243733/1097778320692740096
|
||||
$this->app->singleton('cache', function ($app) {
|
||||
return new $this->app['config']['tenancy.cache.manager']($app);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ use Stancl\Tenancy\CacheManager;
|
|||
|
||||
beforeEach(function () {
|
||||
$this->mockConsoleOutput = false;
|
||||
CacheManager::$addTags = false;
|
||||
config(['cache.default' => $cacheDriver = 'redis']);
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
|
||||
|
|
@ -57,7 +56,6 @@ beforeEach(function () {
|
|||
});
|
||||
|
||||
afterEach(function () {
|
||||
CacheManager::$addTags = false;
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
|
|||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
CacheManager::$addTags = false;
|
||||
config(['cache.default' => $cacheDriver = 'redis']);
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
|
||||
|
|
|
|||
|
|
@ -24,8 +24,6 @@ beforeEach(function () {
|
|||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
|
||||
config('tenancy.cache.manager')::$addTags = false;
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
|
@ -35,28 +33,6 @@ afterEach(function () {
|
|||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
});
|
||||
|
||||
test('Tenancy overrides CacheManager', function() {
|
||||
$tenancyCacheManager = config('tenancy.cache.manager');
|
||||
|
||||
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 () {
|
||||
$originalPrefix = config('cache.prefix');
|
||||
$prefixBase = config('tenancy.cache.prefix_base');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue