1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 09:34:05 +00:00
tenancy/src/StorageDrivers/Database/CachedTenantResolver.php
2020-03-14 19:08:26 +01:00

67 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\StorageDrivers\Database;
use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Config\Repository as ConfigRepository;
use Stancl\Tenancy\Tenant;
class CachedTenantResolver
{
/** @var CacheRepository */
protected $cache;
/** @var ConfigRepository */
protected $config;
public function __construct(CacheManager $cacheManager, ConfigRepository $config)
{
$this->cache = $cacheManager->store($config->get('tenancy.storage_drivers.db.cache_store'));
$this->config = $config;
}
protected function ttl(): int
{
return $this->config->get('tenancy.storage_drivers.db.cache_ttl');
}
public function getTenantIdByDomain(string $domain, Closure $query): string
{
return $this->cache->remember('_tenancy_domain_to_id:' . $domain, $this->ttl(), $query);
}
public function findById(string $id, Closure $dataQuery, Closure $domainsQuery): Tenant
{
$data = $this->cache->remember('_tenancy_id_to_data:' . $id, $this->ttl(), $dataQuery);
$domains = $this->cache->remember('_tenancy_id_to_domains:' . $id, $this->ttl(), $domainsQuery);
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);
}
}
}