mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
CachedTenantResolver
This commit is contained in:
parent
e6e4548a22
commit
d7536ce0af
3 changed files with 77 additions and 0 deletions
38
src/Resolvers/CachedTenantResolver.php
Normal file
38
src/Resolvers/CachedTenantResolver.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Resolvers;
|
||||
|
||||
use Illuminate\Cache\Repository;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantResolver;
|
||||
|
||||
class CachedTenantResolver implements TenantResolver
|
||||
{
|
||||
/** @var Repository */
|
||||
protected $cache;
|
||||
|
||||
public function __construct(Repository $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function resolve(...$args): Tenant
|
||||
{
|
||||
$resolverClass = $args[0];
|
||||
$data = $args[1];
|
||||
|
||||
/** @var TenantResolver $resolver */
|
||||
$resolver = app($resolverClass);
|
||||
$encodedData = json_encode($data);
|
||||
|
||||
if ($this->cache->has($key = "_tenancy_resolver:$resolverClass:$encodedData")) {
|
||||
return $this->cache->get($key);
|
||||
}
|
||||
|
||||
$this->cache->put($key,
|
||||
$resolved = $resolver->resolve(...$data)
|
||||
);
|
||||
|
||||
return $resolved;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue