originalPrefix = $this->config->get('cache.prefix'); $prefix = $this->generatePrefix($tenant); foreach (static::$tenantCacheStores as $store) { $this->setCachePrefix($store, $prefix); // Now that the store uses the passed prefix // Set the configured prefix back to the default one $this->config->set('cache.prefix', $this->originalPrefix); } } public function revert(): void { foreach (static::$tenantCacheStores as $store) { $this->setCachePrefix($store, $this->originalPrefix); } } protected function setCachePrefix(string $driver, string|null $prefix): void { $this->config->set('cache.prefix', $prefix); // Refresh driver's store to make the driver use the current prefix $this->refreshStore($driver); // 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 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; } /** * Refresh cache driver's store. */ protected function refreshStore(string $driver): void { $newStore = $this->cacheManager->resolve($driver)->getStore(); /** @var Repository $repository */ $repository = $this->cacheManager->driver($driver); $repository->setStore($newStore); } }