Skip to content

Commit

Permalink
Merge pull request #28445 from sowbiba/clear-module-cache-all-shops
Browse files Browse the repository at this point in the history
Allow clearing modules cache for all shops
  • Loading branch information
sowbiba committed Jun 1, 2022
2 parents 90a915b + ad66e44 commit edcf2ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
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 @@ -176,19 +176,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

0 comments on commit edcf2ff

Please sign in to comment.