1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

Rewrite cached resolver logic to allow for cache invalidation logic

This commit is contained in:
Samuel Štancl 2020-06-02 20:10:17 +02:00
parent b176481cdf
commit 5d94727ddd
15 changed files with 189 additions and 117 deletions

2
.gitattributes vendored
View file

@ -10,7 +10,7 @@
/.styleci.yml export-ignore /.styleci.yml export-ignore
/docker-compose.yml export-ignore /docker-compose.yml export-ignore
/Dockerfile export-ignore /Dockerfile export-ignore
/fulltest export-ignore /test export-ignore
/phpunit.xml export-ignore /phpunit.xml export-ignore
/.editorconfig export-ignore /.editorconfig export-ignore
/.coverage.xml export-ignore /.coverage.xml export-ignore

View file

@ -6,6 +6,10 @@ namespace Stancl\Tenancy\Contracts;
/** /**
* @see \Stancl\Tenancy\Database\Models\Tenant * @see \Stancl\Tenancy\Database\Models\Tenant
*
* @method mixed __call() IDE support. This will be a model.
* @method mixed __callStatic() IDE support. This will be a model.
* @mixin \Illuminate\Database\Eloquent\Model
*/ */
interface Tenant interface Tenant
{ {

View file

@ -0,0 +1,28 @@
<?php
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
use Stancl\Tenancy\Resolvers;
trait InvalidatesResolverCache
{
public static $resolvers = [
Resolvers\DomainTenantResolver::class,
Resolvers\PathTenantResolver::class,
Resolvers\RequestDataTenantResolver::class,
];
public static function bootInvalidatesResolverCache()
{
static::saved(function (Tenant $tenant) {
foreach (static::$resolvers as $resolver) {
/** @var CachedTenantResolver $resolver */
$resolver = app($resolver);
$resolver->invalidateCache($tenant);
}
});
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Stancl\Tenancy\Database\Concerns;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
use Stancl\Tenancy\Resolvers;
/**
* Meant to be used on models that belong to tenants.
*/
trait InvalidatesTenantsResolverCache
{
public static $resolvers = [
Resolvers\DomainTenantResolver::class,
Resolvers\PathTenantResolver::class,
Resolvers\RequestDataTenantResolver::class,
];
public static function bootInvalidatesTenantsResolverCache()
{
static::saved(function (Model $model) {
foreach (static::$resolvers as $resolver) {
/** @var CachedTenantResolver $resolver */
$resolver = app($resolver);
$resolver->invalidateCache($model->tenant);
}
});
}
}

View file

@ -19,7 +19,8 @@ use Stancl\Tenancy\Events;
class Domain extends Model implements Contracts\Domain class Domain extends Model implements Contracts\Domain
{ {
use Concerns\CentralConnection, use Concerns\CentralConnection,
Concerns\EnsuresDomainIsNotOccupied; Concerns\EnsuresDomainIsNotOccupied,
Concerns\InvalidatesTenantsResolverCache;
protected $guarded = []; protected $guarded = [];

View file

@ -25,7 +25,8 @@ class Tenant extends Model implements Contracts\Tenant
Concerns\GeneratesIds, Concerns\GeneratesIds,
Concerns\HasDataColumn, Concerns\HasDataColumn,
Concerns\HasInternalKeys, Concerns\HasInternalKeys,
Concerns\TenantRun; Concerns\TenantRun,
Concerns\InvalidatesResolverCache;
protected $table = 'tenants'; protected $table = 'tenants';
protected $primaryKey = 'id'; protected $primaryKey = 'id';

View file

@ -14,15 +14,6 @@ abstract class IdentificationMiddleware
/** @var callable */ /** @var callable */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;
@ -32,15 +23,9 @@ abstract class IdentificationMiddleware
public function initializeTenancy($request, $next, ...$resolverArguments) public function initializeTenancy($request, $next, ...$resolverArguments)
{ {
try { try {
if (static::$shouldCache) {
app(CachedTenantResolver::class)->resolve(
get_class($this->resolver), $resolverArguments, static::$cacheTTL, static::$cacheStore
);
} else {
$this->tenancy->initialize( $this->tenancy->initialize(
$this->resolver->resolve(...$resolverArguments) $this->resolver->resolve(...$resolverArguments)
); );
}
} catch (TenantCouldNotBeIdentifiedException $e) { } catch (TenantCouldNotBeIdentifiedException $e) {
$onFail = static::$onFail ?? function ($e) { $onFail = static::$onFail ?? function ($e) {
throw $e; throw $e;

View file

@ -13,15 +13,6 @@ class InitializeTenancyByDomain extends IdentificationMiddleware
/** @var callable|null */ /** @var callable|null */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -16,15 +16,6 @@ class InitializeTenancyByPath extends IdentificationMiddleware
/** @var callable|null */ /** @var callable|null */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -20,15 +20,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
/** @var callable|null */ /** @var callable|null */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -26,15 +26,6 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain
/** @var callable|null */ /** @var callable|null */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** /**
* Handle an incoming request. * Handle an incoming request.
* *

View file

@ -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;
}
}

View 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;
}

View file

@ -6,14 +6,22 @@ namespace Stancl\Tenancy\Resolvers;
use Illuminate\Routing\Route; use Illuminate\Routing\Route;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
class PathTenantResolver implements TenantResolver class PathTenantResolver extends Contracts\CachedTenantResolver
{ {
public static $tenantParameterName = 'tenant'; 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 */ /** @var Route $route */
$route = $args[0]; $route = $args[0];
@ -28,4 +36,11 @@ class PathTenantResolver implements TenantResolver
throw new TenantCouldNotBeIdentifiedByPathException($id); throw new TenantCouldNotBeIdentifiedByPathException($id);
} }
public function getArgsForTenant(Tenant $tenant): array
{
return [
[$tenant->id],
];
}
} }

View file

@ -5,12 +5,20 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers; namespace Stancl\Tenancy\Resolvers;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException; 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]; $payload = $args[0];
@ -20,4 +28,11 @@ class RequestDataTenantResolver implements TenantResolver
throw new TenantCouldNotBeIdentifiedByRequestDataException($payload); throw new TenantCouldNotBeIdentifiedByRequestDataException($payload);
} }
public function getArgsForTenant(Tenant $tenant): array
{
return [
[$tenant->id],
];
}
} }