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

Begin work on cached lookup

This commit is contained in:
Samuel Štancl 2020-03-14 00:16:57 +01:00
parent 142912edc5
commit c52d12cb99
6 changed files with 149 additions and 10 deletions

View file

@ -0,0 +1,45 @@
<?php
namespace Stancl\Tenancy\StorageDrivers\Database;
use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Cache\Store;
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);
}
// todo update cache on writes to data & domains
}

View file

@ -32,12 +32,16 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
/** @var DomainRepository */
protected $domains;
/** @var CachedTenantResolver */
protected $cache;
/** @var Tenant The default tenant. */
protected $tenant;
public function __construct(Application $app, ConfigRepository $config)
public function __construct(Application $app, ConfigRepository $config, CachedTenantResolver $cache)
{
$this->app = $app;
$this->cache = $cache;
$this->centralDatabase = $this->getCentralConnection();
$this->tenants = new TenantRepository($config);
$this->domains = new DomainRepository($config);
@ -60,7 +64,16 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
public function findByDomain(string $domain): Tenant
{
$id = $this->domains->getTenantIdByDomain($domain);
$query = function () use ($domain) {
return $this->domains->getTenantIdByDomain($domain);
};
if ($this->usesCache()) {
$id = $this->cache->getTenantIdByDomain($domain, $query);
} else {
$id = $query();
}
if (! $id) {
throw new TenantCouldNotBeIdentifiedException($domain);
}
@ -70,14 +83,25 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
public function findById(string $id): Tenant
{
$tenant = $this->tenants->find($id);
$dataQuery = function () use ($id) {
return $this->tenants->decodeData($this->tenants->find($id));
};
$domainsQuery = function () use ($id) {
return $this->domains->getTenantDomains($id);
};
if (! $tenant) {
if ($this->usesCache()) {
return $this->cache->findById($id, $dataQuery, $domainsQuery);
} else {
$data = $dataQuery();
}
if (! $data) {
throw new TenantDoesNotExistException($id);
}
return Tenant::fromStorage($this->tenants->decodeData($tenant))
->withDomains($this->domains->getTenantDomains($id));
return Tenant::fromStorage($data)
->withDomains($domainsQuery());
}
/**
@ -191,4 +215,10 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
{
$this->tenants->deleteMany($keys, $tenant ?? $this->currentTenant());
}
public function usesCache(): bool
{
// null is also truthy here
return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== false;
}
}