1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 08:24:05 +00:00

Finish cache invalidation

This commit is contained in:
Samuel Štancl 2020-03-14 19:08:26 +01:00
parent a6102be1a0
commit 2c1cb92745
3 changed files with 49 additions and 8 deletions

View file

@ -64,6 +64,4 @@ class CachedTenantResolver
$this->cache->forget('_tenancy_domain_to_id:' . $domain);
}
}
// todo update cache on writes to data & domains
}

View file

@ -168,10 +168,17 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
public function deleteTenant(Tenant $tenant): void
{
$originalDomains = $this->domains->getTenantDomains($tenant);
$this->centralDatabase->transaction(function () use ($tenant) {
$this->tenants->where('id', $tenant->id)->delete();
$this->domains->where('tenant_id', $tenant->id)->delete();
});
if ($this->usesCache()) {
$this->cache->invalidateTenant($tenant->id);
$this->cache->invalidateDomainToIdMapping($originalDomains);
}
}
/**

View file

@ -73,14 +73,33 @@ class CachedResolverTest extends TestCase
}
/** @test */
public function modifying_tenants_domains_updates_domains_in_the_cached_domain_to_id_mapping()
public function modifying_tenant_domains_invalidates_the_cached_domain_to_id_mapping()
{
// todo adding domain adds mapping
// todo removing domain removes mapping
$tenant = Tenant::new()
->withDomains(['foo.localhost', 'bar.localhost'])
->save();
// queried
$this->assertSame($tenant->id, tenancy()->findByDomain('foo.localhost')->id);
$this->assertSame($tenant->id, tenancy()->findByDomain('bar.localhost')->id);
// assert cache set
$this->assertSame($tenant->id, Cache::get('_tenancy_domain_to_id:foo.localhost'));
$this->assertSame($tenant->id, Cache::get('_tenancy_domain_to_id:bar.localhost'));
$tenant
->removeDomains(['foo.localhost', 'bar.localhost'])
->addDomains(['xyz.localhost'])
->save();
// assert neither domain is cached
$this->assertSame(null, Cache::get('_tenancy_domain_to_id:foo.localhost'));
$this->assertSame(null, Cache::get('_tenancy_domain_to_id:bar.localhost'));
$this->assertSame(null, Cache::get('_tenancy_domain_to_id:xyz.localhost'));
}
/** @test */
public function modifying_tenants_data_updates_data_in_the_cached_id_to_tenant_data_mapping()
public function modifying_tenants_data_invalidates_tenant_data_cache()
{
$tenant = Tenant::new()->withData(['foo' => 'bar'])->save();
@ -103,7 +122,7 @@ class CachedResolverTest extends TestCase
}
/** @test */
public function modifying_tenants_domains_updates_domains_in_the_cached_id_to_tenant_domains_mapping()
public function modifying_tenants_domains_invalidates_tenant_domain_cache()
{
$tenant = Tenant::new()
->withData(['foo' => 'bar'])
@ -121,5 +140,22 @@ class CachedResolverTest extends TestCase
$this->assertEquals(['foo.localhost', 'bar.localhost'], tenancy()->find($tenant->id)->domains);
}
// todo deleting tenant invalidates all caches
/** @test */
public function deleting_a_tenant_invalidates_all_caches()
{
$tenant = Tenant::new()
->withData(['foo' => 'bar'])
->withDomains(['foo.localhost'])
->save();
tenancy()->findByDomain('foo.localhost');
$this->assertEquals($tenant->id, Cache::get('_tenancy_domain_to_id:foo.localhost'));
$this->assertEquals($tenant->data, Cache::get('_tenancy_id_to_data:' . $tenant->id));
$this->assertEquals(['foo.localhost'], Cache::get('_tenancy_id_to_domains:' . $tenant->id));
$tenant->delete();
$this->assertEquals(null, Cache::get('_tenancy_domain_to_id:foo.localhost'));
$this->assertEquals(null, Cache::get('_tenancy_id_to_data:' . $tenant->id));
$this->assertEquals(null, Cache::get('_tenancy_id_to_domains:' . $tenant->id));
}
}