mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 06:44:04 +00:00
Invalidate resolver cache on delete (#1328)
This commit is contained in:
parent
e46355fb9d
commit
d98a170fbd
3 changed files with 65 additions and 4 deletions
|
|
@ -18,13 +18,16 @@ trait InvalidatesResolverCache
|
|||
|
||||
public static function bootInvalidatesResolverCache()
|
||||
{
|
||||
static::saved(function (Tenant $tenant) {
|
||||
$invalidateCache = static function (Tenant $tenant) {
|
||||
foreach (static::$resolvers as $resolver) {
|
||||
/** @var CachedTenantResolver $resolver */
|
||||
$resolver = app($resolver);
|
||||
|
||||
$resolver->invalidateCache($tenant);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
static::saved($invalidateCache);
|
||||
static::deleting($invalidateCache);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,13 +21,16 @@ trait InvalidatesTenantsResolverCache
|
|||
|
||||
public static function bootInvalidatesTenantsResolverCache()
|
||||
{
|
||||
static::saved(function (Model $model) {
|
||||
$invalidateCache = static function (Model $model) {
|
||||
foreach (static::$resolvers as $resolver) {
|
||||
/** @var CachedTenantResolver $resolver */
|
||||
$resolver = app($resolver);
|
||||
|
||||
$resolver->invalidateCache($model->tenant);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
static::saved($invalidateCache);
|
||||
static::deleting($invalidateCache);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
|
|
@ -80,6 +81,33 @@ class CachedTenantResolverTest extends TestCase
|
|||
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function cache_is_invalidated_when_the_tenant_is_deleted()
|
||||
{
|
||||
$tenant = Tenant::create();
|
||||
$tenant->createDomain([
|
||||
'domain' => 'acme',
|
||||
]);
|
||||
|
||||
DB::enableQueryLog();
|
||||
|
||||
DomainTenantResolver::$shouldCache = true;
|
||||
|
||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||
DB::flushQueryLog();
|
||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||
$this->assertEmpty(DB::getQueryLog()); // empty
|
||||
|
||||
$tenant->delete();
|
||||
DB::flushQueryLog();
|
||||
|
||||
$this->assertThrows(function () {
|
||||
app(DomainTenantResolver::class)->resolve('acme');
|
||||
}, TenantCouldNotBeIdentifiedOnDomainException::class);
|
||||
|
||||
$this->assertNotEmpty(DB::getQueryLog()); // not empty - cache cleared so the DB was queried
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function cache_is_invalidated_when_a_tenants_domain_is_changed()
|
||||
{
|
||||
|
|
@ -109,4 +137,31 @@ class CachedTenantResolverTest extends TestCase
|
|||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('bar')));
|
||||
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function cache_is_invalidated_when_a_tenants_domain_is_deleted()
|
||||
{
|
||||
$tenant = Tenant::create();
|
||||
$tenant->createDomain([
|
||||
'domain' => 'acme',
|
||||
]);
|
||||
|
||||
DB::enableQueryLog();
|
||||
|
||||
DomainTenantResolver::$shouldCache = true;
|
||||
|
||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||
DB::flushQueryLog();
|
||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||
$this->assertEmpty(DB::getQueryLog()); // empty
|
||||
|
||||
$tenant->domains->first()->delete();
|
||||
DB::flushQueryLog();
|
||||
|
||||
$this->assertThrows(function () {
|
||||
app(DomainTenantResolver::class)->resolve('acme');
|
||||
}, TenantCouldNotBeIdentifiedOnDomainException::class);
|
||||
|
||||
$this->assertNotEmpty(DB::getQueryLog()); // not empty - cache cleared so the DB was queried
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue