Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clearing modules cache for all shops #28445

Merged
merged 3 commits into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Core/Module/EventSubscriber.php
Expand Up @@ -69,7 +69,7 @@ public static function getSubscribedEvents()
public function onModuleStateChanged(ModuleManagementEvent $event): void
{
$moduleName = $event->getModule()->get('name');
$this->moduleRepository->clearCache($moduleName);
$this->moduleRepository->clearCache($moduleName, true);
}

public function onModuleInstalledOrUninstalled(ModuleManagementEvent $event): void
Expand Down
40 changes: 35 additions & 5 deletions src/Core/Module/ModuleRepository.php
Expand Up @@ -175,19 +175,49 @@ public function setActionUrls(ModuleCollection $collection): ModuleCollection
return $this->adminModuleDataProvider->setActionUrls($collection);
}

public function clearCache(?string $moduleName = null): bool
/**
* @param string|null $moduleName The module to clear the cache for. If the name is null, the cache will be cleared for all modules.
* @param bool $allShops Default to false. If the value is true, the cache will be cleared for all the active shops. If not it will be cleared only for the shop in the context.
*
* @return bool
*/
public function clearCache(?string $moduleName = null, bool $allShops = false): bool
{
$this->installedModules = null;
if ($moduleName !== null && $this->cacheProvider->contains($this->getCacheKey($moduleName))) {
return $this->cacheProvider->delete($this->getCacheKey($moduleName));
if ($moduleName !== null) {
if ($allShops) {
foreach (Shop::getShops(true, null, true) as $shopId) {
$cacheKey = $this->getCacheKey($moduleName, $shopId);
if ($this->cacheProvider->contains($cacheKey)) {
if (!$this->cacheProvider->delete($cacheKey)) {
return false;
}
}
}

return true;
} else {
$cacheKey = $this->getCacheKey($moduleName);
if ($this->cacheProvider->contains($cacheKey)) {
return $this->cacheProvider->delete($cacheKey);
}
}
}

return $this->cacheProvider->deleteAll();
}

private function getCacheKey(string $moduleName): string
/**
* @param string $moduleName
* @param int|null $shopId If this parameter is given, the key returned will be the one for the shop. Otherwise, it will be the cache key for the shop in the context.
*
* @return string
*/
protected function getCacheKey(string $moduleName, ?int $shopId = null): string
{
return $moduleName . implode('-', Shop::getContextListShopID());
$shop = $shopId ? [$shopId] : Shop::getContextListShopID();

return $moduleName . implode('-', $shop);
}

private function getModuleAttributes(string $moduleName, bool $isValid): array
Expand Down