mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
Rewrite cached resolver logic to allow for cache invalidation logic
This commit is contained in:
parent
b176481cdf
commit
5d94727ddd
15 changed files with 189 additions and 117 deletions
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Resolvers;
|
||||
|
||||
use Illuminate\Contracts\Cache\Factory;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
|
||||
class CachedTenantResolver implements TenantResolver
|
||||
{
|
||||
/** @var CacheManager */
|
||||
protected $cache;
|
||||
|
||||
public function __construct(Factory $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function resolve(...$args): Tenant
|
||||
{
|
||||
$resolverClass = $args[0];
|
||||
$data = $args[1];
|
||||
$ttl = $args[2] ?? null;
|
||||
$cacheStore = $args[3] ?? null;
|
||||
|
||||
/** @var TenantResolver $resolver */
|
||||
$resolver = app($resolverClass);
|
||||
$encodedData = json_encode($data);
|
||||
|
||||
$cache = $this->cache->store($cacheStore);
|
||||
|
||||
if ($cache->has($key = "_tenancy_resolver:$resolverClass:$encodedData")) {
|
||||
return $cache->get($key);
|
||||
}
|
||||
|
||||
$resolved = $resolver->resolve(...$data);
|
||||
$cache->put($key, $resolved, $ttl);
|
||||
|
||||
return $resolved;
|
||||
}
|
||||
}
|
||||
70
src/Resolvers/Contracts/CachedTenantResolver.php
Normal file
70
src/Resolvers/Contracts/CachedTenantResolver.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Resolvers\Contracts;
|
||||
|
||||
use Illuminate\Contracts\Cache\Factory;
|
||||
use Illuminate\Contracts\Cache\Repository;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
use Stancl\Tenancy\Database\Models\Domain;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
|
||||
abstract class CachedTenantResolver implements TenantResolver
|
||||
{
|
||||
/** @var bool */
|
||||
public static $shouldCache = false;
|
||||
|
||||
/** @var int */
|
||||
public static $cacheTTL = 3600; // seconds
|
||||
|
||||
/** @var string|null */
|
||||
public static $cacheStore = null; // default
|
||||
|
||||
/** @var Repository */
|
||||
protected $cache;
|
||||
|
||||
public function __construct(Factory $cache)
|
||||
{
|
||||
$this->cache = $cache->store(static::$cacheStore);
|
||||
}
|
||||
|
||||
public function resolve(...$args): Tenant
|
||||
{
|
||||
if (! static::$shouldCache) {
|
||||
return $this->resolveWithoutCache(...$args);
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey(...$args);
|
||||
|
||||
if ($this->cache->has($key)) {
|
||||
return $this->cache->get($key);
|
||||
}
|
||||
|
||||
$tenant = $this->resolveWithoutCache(...$args);
|
||||
$this->cache->put($key, $tenant, static::$cacheTTL);
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
public function invalidateCache(Tenant $tenant): void
|
||||
{
|
||||
foreach ($this->getArgsForTenant($tenant) as $args) {
|
||||
$this->cache->forget($this->getCacheKey(...$args));
|
||||
}
|
||||
}
|
||||
|
||||
public function getCacheKey(...$args): string
|
||||
{
|
||||
return '_tenancy_resolver:' . static::class . ':' . json_encode($args);
|
||||
}
|
||||
|
||||
abstract public function resolveWithoutCache(...$args): Tenant;
|
||||
|
||||
/**
|
||||
* Get all the arg combinations for resolve() that can be used to find this tenant.
|
||||
*
|
||||
* @param Tenant $tenant
|
||||
* @return array[]
|
||||
*/
|
||||
abstract public function getArgsForTenant(Tenant $tenant): array;
|
||||
}
|
||||
|
|
@ -6,14 +6,22 @@ namespace Stancl\Tenancy\Resolvers;
|
|||
|
||||
use Illuminate\Routing\Route;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
|
||||
|
||||
class PathTenantResolver implements TenantResolver
|
||||
class PathTenantResolver extends Contracts\CachedTenantResolver
|
||||
{
|
||||
public static $tenantParameterName = 'tenant';
|
||||
|
||||
public function resolve(...$args): Tenant
|
||||
/** @var bool */
|
||||
public static $shouldCache = false;
|
||||
|
||||
/** @var int */
|
||||
public static $cacheTTL = 3600; // seconds
|
||||
|
||||
/** @var string|null */
|
||||
public static $cacheStore = null; // default
|
||||
|
||||
public function resolveWithoutCache(...$args): Tenant
|
||||
{
|
||||
/** @var Route $route */
|
||||
$route = $args[0];
|
||||
|
|
@ -28,4 +36,11 @@ class PathTenantResolver implements TenantResolver
|
|||
|
||||
throw new TenantCouldNotBeIdentifiedByPathException($id);
|
||||
}
|
||||
|
||||
public function getArgsForTenant(Tenant $tenant): array
|
||||
{
|
||||
return [
|
||||
[$tenant->id],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,20 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Resolvers;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
|
||||
|
||||
class RequestDataTenantResolver implements TenantResolver
|
||||
class RequestDataTenantResolver extends Contracts\CachedTenantResolver
|
||||
{
|
||||
public function resolve(...$args): Tenant
|
||||
/** @var bool */
|
||||
public static $shouldCache = false;
|
||||
|
||||
/** @var int */
|
||||
public static $cacheTTL = 3600; // seconds
|
||||
|
||||
/** @var string|null */
|
||||
public static $cacheStore = null; // default
|
||||
|
||||
public function resolveWithoutCache(...$args): Tenant
|
||||
{
|
||||
$payload = $args[0];
|
||||
|
||||
|
|
@ -20,4 +28,11 @@ class RequestDataTenantResolver implements TenantResolver
|
|||
|
||||
throw new TenantCouldNotBeIdentifiedByRequestDataException($payload);
|
||||
}
|
||||
|
||||
public function getArgsForTenant(Tenant $tenant): array
|
||||
{
|
||||
return [
|
||||
[$tenant->id],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue