mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:34:04 +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
24
.gitattributes
vendored
24
.gitattributes
vendored
|
|
@ -2,16 +2,16 @@
|
||||||
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
|
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
|
||||||
|
|
||||||
# Ignore all test and documentation with "export-ignore".
|
# Ignore all test and documentation with "export-ignore".
|
||||||
/.github export-ignore
|
/.github export-ignore
|
||||||
/art export-ignore
|
/art export-ignore
|
||||||
/tests export-ignore
|
/tests export-ignore
|
||||||
/.gitattributes export-ignore
|
/.gitattributes export-ignore
|
||||||
/.gitignore export-ignore
|
/.gitignore export-ignore
|
||||||
/.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
|
||||||
/coverage export-ignore
|
/coverage export-ignore
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
{
|
{
|
||||||
|
|
|
||||||
28
src/Database/Concerns/InvalidatesResolverCache.php
Normal file
28
src/Database/Concerns/InvalidatesResolverCache.php
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/Database/Concerns/InvalidatesTenantsResolverCache.php
Normal file
32
src/Database/Concerns/InvalidatesTenantsResolverCache.php
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 = [];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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';
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
$this->tenancy->initialize(
|
||||||
app(CachedTenantResolver::class)->resolve(
|
$this->resolver->resolve(...$resolverArguments)
|
||||||
get_class($this->resolver), $resolverArguments, static::$cacheTTL, static::$cacheStore
|
);
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->tenancy->initialize(
|
|
||||||
$this->resolver->resolve(...$resolverArguments)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (TenantCouldNotBeIdentifiedException $e) {
|
} catch (TenantCouldNotBeIdentifiedException $e) {
|
||||||
$onFail = static::$onFail ?? function ($e) {
|
$onFail = static::$onFail ?? function ($e) {
|
||||||
throw $e;
|
throw $e;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -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 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],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue