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

Update cache prefixing and tests

This commit is contained in:
lukinovec 2023-04-17 14:34:54 +02:00
parent 13b85a2d3b
commit 52d9643535
2 changed files with 32 additions and 30 deletions

View file

@ -28,12 +28,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{
$this->originalPrefix = $this->config->get('cache.prefix');
// Use default prefix generator if the prefix generator isn't set
static::$prefixGenerator ??= function (Tenant $tenant) {
return $this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
};
$prefix = (static::$prefixGenerator)($tenant);
$prefix = $this->generatePrefix($tenant);
foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $prefix);
@ -49,8 +44,6 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $this->originalPrefix);
}
static::$prefixGenerator = null;
}
protected function setCachePrefix(string $driver, string|null $prefix): void
@ -65,6 +58,13 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
Cache::clearResolvedInstances();
}
public function generatePrefix(Tenant $tenant): string
{
$defaultPrefix = $this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
return static::$prefixGenerator ? (static::$prefixGenerator)($tenant) : $defaultPrefix;
}
public static function generatePrefixUsing(Closure $prefixGenerator): void
{
static::$prefixGenerator = $prefixGenerator;