From 2c1cb927458f8d67b3952b48c8cef0359c41e6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 14 Mar 2020 19:08:26 +0100 Subject: [PATCH] Finish cache invalidation --- .../Database/CachedTenantResolver.php | 2 - .../Database/DatabaseStorageDriver.php | 7 +++ tests/CachedResolverTest.php | 48 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/StorageDrivers/Database/CachedTenantResolver.php b/src/StorageDrivers/Database/CachedTenantResolver.php index ce7090ea..ecf66989 100644 --- a/src/StorageDrivers/Database/CachedTenantResolver.php +++ b/src/StorageDrivers/Database/CachedTenantResolver.php @@ -64,6 +64,4 @@ class CachedTenantResolver $this->cache->forget('_tenancy_domain_to_id:' . $domain); } } - - // todo update cache on writes to data & domains } diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index 42698e3c..3dba4344 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -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); + } } /** diff --git a/tests/CachedResolverTest.php b/tests/CachedResolverTest.php index 4f618b8b..f85418e9 100644 --- a/tests/CachedResolverTest.php +++ b/tests/CachedResolverTest.php @@ -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)); + } }