mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 21:14:03 +00:00
wip cache invalidation
This commit is contained in:
parent
a99f5d916d
commit
a6102be1a0
4 changed files with 124 additions and 20 deletions
|
|
@ -42,5 +42,28 @@ class CachedTenantResolver
|
|||
return Tenant::fromStorage($data)->withDomains($domains);
|
||||
}
|
||||
|
||||
public function invalidateTenant(string $id): void
|
||||
{
|
||||
$this->invalidateTenantData($id);
|
||||
$this->invalidateTenantDomains($id);
|
||||
}
|
||||
|
||||
public function invalidateTenantData(string $id): void
|
||||
{
|
||||
$this->cache->forget('_tenancy_id_to_data:' . $id);
|
||||
}
|
||||
|
||||
public function invalidateTenantDomains(string $id): void
|
||||
{
|
||||
$this->cache->forget('_tenancy_id_to_domains:' . $id);
|
||||
}
|
||||
|
||||
public function invalidateDomainToIdMapping(array $domains): void
|
||||
{
|
||||
foreach ($domains as $domain) {
|
||||
$this->cache->forget('_tenancy_domain_to_id:' . $domain);
|
||||
}
|
||||
}
|
||||
|
||||
// todo update cache on writes to data & domains
|
||||
}
|
||||
|
|
|
|||
|
|
@ -152,11 +152,18 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
|
|||
|
||||
public function updateTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
$originalDomains = $this->domains->getTenantDomains($tenant);
|
||||
|
||||
$this->centralDatabase->transaction(function () use ($tenant, $originalDomains) {
|
||||
$this->tenants->updateTenant($tenant);
|
||||
|
||||
$this->domains->updateTenantDomains($tenant);
|
||||
$this->domains->updateTenantDomains($tenant, $originalDomains);
|
||||
});
|
||||
|
||||
if ($this->usesCache()) {
|
||||
$this->cache->invalidateTenant($tenant->id);
|
||||
$this->cache->invalidateDomainToIdMapping($originalDomains);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteTenant(Tenant $tenant): void
|
||||
|
|
@ -203,17 +210,32 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
|
|||
|
||||
public function put(string $key, $value, Tenant $tenant = null): void
|
||||
{
|
||||
$this->tenants->put($key, $value, $tenant ?? $this->currentTenant());
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
$this->tenants->put($key, $value, $tenant);
|
||||
|
||||
if ($this->usesCache()) {
|
||||
$this->cache->invalidateTenantData($tenant->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function putMany(array $kvPairs, Tenant $tenant = null): void
|
||||
{
|
||||
$this->tenants->putMany($kvPairs, $tenant ?? $this->currentTenant());
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
$this->tenants->putMany($kvPairs, $tenant);
|
||||
|
||||
if ($this->usesCache()) {
|
||||
$this->cache->invalidateTenantData($tenant->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void
|
||||
{
|
||||
$this->tenants->deleteMany($keys, $tenant ?? $this->currentTenant());
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
$this->tenants->deleteMany($keys, $tenant);
|
||||
|
||||
if ($this->usesCache()) {
|
||||
$this->cache->invalidateTenantData($tenant->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function usesCache(): bool
|
||||
|
|
|
|||
|
|
@ -33,9 +33,8 @@ class DomainRepository extends Repository
|
|||
}, $tenant->domains));
|
||||
}
|
||||
|
||||
public function updateTenantDomains(Tenant $tenant)
|
||||
public function updateTenantDomains(Tenant $tenant, array $originalDomains)
|
||||
{
|
||||
$originalDomains = $this->getTenantDomains($tenant);
|
||||
$deletedDomains = array_diff($originalDomains, $tenant->domains);
|
||||
$newDomains = array_diff($tenant->domains, $originalDomains);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue