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

Add customizing cache store prefixes

This commit is contained in:
lukinovec 2023-03-14 17:22:56 +01:00
parent 660b772fe3
commit 8348389fb3

View file

@ -4,17 +4,21 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers; namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\CacheManager; use Closure;
use Illuminate\Cache\Repository; use Illuminate\Cache\Repository;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Cache\CacheManager;
use Stancl\Tenancy\Contracts\Tenant;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Illuminate\Contracts\Config\Repository as ConfigRepository;
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{ {
protected string|null $originalPrefix = null; protected array $originalPrefixes = []; // E.g. 'redis' => 'redis_prefix_'
public static array $tenantCacheStores = []; public static array $tenantCacheStores = []; // E.g. 'redis'
public static array $prefixGenerators = [
// driverName => Closure(Tenant $tenant)
];
public function __construct( public function __construct(
protected ConfigRepository $config, protected ConfigRepository $config,
@ -24,32 +28,62 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
public function bootstrap(Tenant $tenant): void public function bootstrap(Tenant $tenant): void
{ {
$this->originalPrefix = $this->config->get('cache.prefix'); // If the user didn't specify the default generator
// Use static::defaultPrefixGenerator() as the default prefix generator
if (! isset(static::$prefixGenerators['default'])) {
static::generatePrefixUsing('default', static::defaultPrefixGenerator($this->config->get('cache.prefix')));
}
$this->setCachePrefix($this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey()); foreach (static::$tenantCacheStores as $store) {
$this->originalPrefixes[$store] = $this->config->get('cache.prefix');
$this->setCachePrefix($store, $this->getStorePrefix($store, $tenant));
}
} }
public function revert(): void public function revert(): void
{ {
$this->setCachePrefix($this->originalPrefix); foreach ($this->originalPrefixes as $driver => $prefix) {
$this->setCachePrefix($driver, $prefix);
}
$this->originalPrefix = null; $this->originalPrefixes = [];
} }
protected function setCachePrefix(string|null $prefix): void public static function defaultPrefixGenerator(string $originalPrefix = ''): Closure
{
return function (Tenant $tenant) use ($originalPrefix) {
return $originalPrefix . config('tenancy.cache.prefix_base') . $tenant->getTenantKey();
};
}
protected function setCachePrefix(string $driver, string|null $prefix): void
{ {
$this->config->set('cache.prefix', $prefix); $this->config->set('cache.prefix', $prefix);
foreach (static::$tenantCacheStores as $driver) { // Refresh driver's store to make the driver use the current prefix
// Refresh driver's store to make the driver use the current prefix $this->refreshStore($driver);
$this->refreshStore($driver);
}
// It is needed when a call to the facade has been made before bootstrapping tenancy // It is needed when a call to the facade has been made before bootstrapping tenancy
// The facade has its own cache, separate from the container // The facade has its own cache, separate from the container
Cache::clearResolvedInstances(); Cache::clearResolvedInstances();
} }
public function getStorePrefix(string $store, Tenant $tenant): string
{
if (isset(static::$prefixGenerators[$store])) {
return static::$prefixGenerators[$store]($tenant);
}
// Use default generator if the store doesn't have a custom generator
return static::$prefixGenerators['default']($tenant);
}
public static function generatePrefixUsing(string $store, Closure $prefixGenerator): void
{
static::$prefixGenerators[$store] = $prefixGenerator;
}
/** /**
* Refresh cache driver's store. * Refresh cache driver's store.
*/ */