-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite cached resolver logic to allow for cache invalidation logic
- Loading branch information
Showing
15 changed files
with
189 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.