mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 11:34:03 +00:00
Enable caching in initialization mw
This commit is contained in:
parent
d7c04d304d
commit
800e8d5a56
7 changed files with 116 additions and 3 deletions
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Middleware;
|
|||
|
||||
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
use Stancl\Tenancy\Resolvers\CachedTenantResolver;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
abstract class IdentificationMiddleware
|
||||
|
|
@ -13,6 +14,15 @@ abstract class IdentificationMiddleware
|
|||
/** @var callable */
|
||||
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 */
|
||||
protected $tenancy;
|
||||
|
||||
|
|
@ -22,9 +32,15 @@ abstract class IdentificationMiddleware
|
|||
public function initializeTenancy($request, $next, ...$resolverArguments)
|
||||
{
|
||||
try {
|
||||
$this->tenancy->initialize(
|
||||
$this->resolver->resolve(...$resolverArguments)
|
||||
);
|
||||
if (static::$shouldCache) {
|
||||
app(CachedTenantResolver::class)->resolve(
|
||||
get_class($this->resolver), $resolverArguments, static::$cacheTTL, static::$cacheStore
|
||||
);
|
||||
} else {
|
||||
$this->tenancy->initialize(
|
||||
$this->resolver->resolve(...$resolverArguments)
|
||||
);
|
||||
}
|
||||
} catch (TenantCouldNotBeIdentifiedException $e) {
|
||||
$onFail = static::$onFail ?? function ($e) {
|
||||
throw $e;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue