Skip to content

Commit

Permalink
Introduce shop pool service and remove invalidateAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
kenariosz committed Apr 23, 2024
1 parent 08dfb5e commit 6c55261
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 152 deletions.
19 changes: 9 additions & 10 deletions source/Internal/Framework/Cache/Command/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ContainerCacheInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -16,11 +16,11 @@
class ClearCacheCommand extends Command
{
public function __construct(
private ShopAdapterInterface $shopAdapter,
private TemplateCacheServiceInterface $templateCacheService,
private ContainerCacheInterface $containerCache,
private ModuleCacheServiceInterface $moduleCacheService,
private ContextInterface $context
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $templateCacheService,
private readonly ContainerCacheInterface $containerCache,
private readonly ShopPoolServiceInterface $shopPoolService,
private readonly ContextInterface $context
) {
parent::__construct();
}
Expand All @@ -32,15 +32,14 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->templateCacheService->invalidateTemplateCache();
$this->templateCacheService->invalidateAllShopsCache();
$this->shopAdapter->invalidateModulesCache();

foreach ($this->context->getAllShopIds() as $shopId) {
$this->containerCache->invalidate($shopId);
$this->shopPoolService->invalidate($shopId);
}

$this->moduleCacheService->invalidateAll();

$output->writeln("<info>Cleared cache files</info>");

return 0;
Expand Down
24 changes: 24 additions & 0 deletions source/Internal/Framework/Cache/Pool/ShopPoolService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool;

use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use Psr\Cache\CacheItemPoolInterface;

class ShopPoolService implements ShopPoolServiceInterface
{
public function __construct(
private readonly CacheItemPoolInterface $cacheItemPool,
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $templateCacheService,
) {
}

public function invalidate(int $shopId): void
{
$this->templateCacheService->invalidateCache($shopId);
$this->shopAdapter->invalidateModulesCache();
$this->cacheItemPool->clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool;

interface ShopPoolServiceInterface
{
public function invalidate(int $shopId);
}
7 changes: 7 additions & 0 deletions source/Internal/Framework/Cache/Pool/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
_defaults:
autowire: true

OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolService
public: true
3 changes: 2 additions & 1 deletion source/Internal/Framework/Cache/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ services:
$directory: '%oxid_cache_directory%'

imports:
- { resource: Command/services.yaml }
- { resource: Command/services.yaml }
- { resource: Pool/services.yaml }
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Module\Cache;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Event\ModuleConfigurationChangedEvent;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Event\FinalizingModuleActivationEvent;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Event\FinalizingModuleDeactivationEvent;
Expand All @@ -17,14 +18,13 @@

class InvalidateModuleCacheEventSubscriber implements EventSubscriberInterface
{
public function __construct(private ModuleCacheServiceInterface $moduleCacheService)
public function __construct(private readonly ShopPoolServiceInterface $shopPoolService)
{
}

public function invalidateModuleCache(
ModuleSetupEvent|ModuleConfigurationChangedEvent $event
): void {
$this->moduleCacheService->invalidate($event->getModuleId(), $event->getShopId());
public function invalidateModuleCache(ModuleSetupEvent|ModuleConfigurationChangedEvent $event): void
{
$this->shopPoolService->invalidate($event->getShopId());
}

public static function getSubscribedEvents(): array
Expand Down
33 changes: 8 additions & 25 deletions source/Internal/Framework/Module/Cache/ModuleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,20 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Module\Cache;

use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Psr\Cache\CacheItemPoolInterface;

class ModuleCache implements ModuleCacheServiceInterface
{
public function __construct(
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $templateCacheService,
private readonly CacheInterface $cache
) {
}

public function invalidate(string $moduleId, int $shopId): void
public function __construct(private readonly CacheItemPoolInterface $cache)
{
$this->templateCacheService->invalidateCache($shopId);
$this->shopAdapter->invalidateModuleCache($moduleId);
$this->cache->clear();
}

public function invalidateAll(): void
public function invalidate(string $key): void
{
$this->templateCacheService->invalidateAllShopsCache();
$this->shopAdapter->invalidateModulesCache();

$this->cache->clear();
$this->cache->deleteItem($key);
}

public function put(string $key, int $shopId, array $data): void
public function put(string $key, array $data): void
{
$cacheModulePathItem = $this->cache->getItem($key);
$cacheModulePathItem->set($data);
Expand All @@ -47,20 +32,18 @@ public function put(string $key, int $shopId, array $data): void
/**
* @throws CacheNotFoundException
*/
public function get(string $key, int $shopId): array
public function get(string $key): array
{
$cacheModulePathItem = $this->cache->getItem($key);

if (!$cacheModulePathItem->isHit()) {
throw new CacheNotFoundException(
"Cache with key '$key' for the shop with id $shopId not found."
);
throw new CacheNotFoundException("Cache with key '$key' not found.");
}

return $cacheModulePathItem->get();
}

public function exists(string $key, int $shopId): bool
public function exists(string $key): bool
{
return $this->cache->getItem($key)->isHit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,11 @@

interface ModuleCacheServiceInterface
{
/**
* Invalidate all module related cache items for a given module and a given shop
*
* @param string $moduleId
* @param int $shopId
*/
public function invalidate(string $moduleId, int $shopId): void;
public function invalidate(string $key): void;

public function invalidateAll(): void;
public function put(string $key, array $data): void;

/**
* @param string $key
* @param int $shopId
* @param array $data
*/
public function put(string $key, int $shopId, array $data): void;
public function get(string $key): array;

/**
* @param string $key
* @param int $shopId
*
* @return array
*/
public function get(string $key, int $shopId): array;

/**
* @param string $key
* @param int $shopId
*
* @return bool
*/
public function exists(string $key, int $shopId): bool;
public function exists(string $key): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
class ActiveModulesDataProvider implements ActiveModulesDataProviderInterface
{
public function __construct(
private ModuleConfigurationDaoInterface $moduleConfigurationDao,
private ModulePathResolverInterface $modulePathResolver,
private ContextInterface $context,
private ModuleCacheServiceInterface $moduleCacheService,
private ActiveClassExtensionChainResolverInterface $activeClassExtensionChainResolver
private readonly ModuleConfigurationDaoInterface $moduleConfigurationDao,
private readonly ModulePathResolverInterface $modulePathResolver,
private readonly ContextInterface $context,
private readonly ModuleCacheServiceInterface $moduleCacheService,
private readonly ActiveClassExtensionChainResolverInterface $activeClassExtensionChainResolver
) {
}

Expand All @@ -42,31 +42,28 @@ public function getModuleIds(): array
/** @inheritDoc */
public function getModulePaths(): array
{
$shopId = $this->context->getCurrentShopId();
$cacheKey = 'absolute_module_paths';

if (!$this->moduleCacheService->exists($cacheKey, $shopId)) {
if (!$this->moduleCacheService->exists($cacheKey)) {
$this->moduleCacheService->put(
$cacheKey,
$shopId,
$this->collectModulePathsForCaching()
);
}
return $this->moduleCacheService->get($cacheKey, $shopId);
return $this->moduleCacheService->get($cacheKey);
}

/** @inheritDoc */
public function getControllers(): array
{
$shopId = $this->context->getCurrentShopId();
$cacheKey = 'controllers';

if (!$this->moduleCacheService->exists($cacheKey, $shopId)) {
$this->moduleCacheService->put($cacheKey, $shopId, $this->collectControllersForCaching());
if (!$this->moduleCacheService->exists($cacheKey)) {
$this->moduleCacheService->put($cacheKey, $this->collectControllersForCaching());
}

return $this->createControllersFromData(
$this->moduleCacheService->get($cacheKey, $shopId)
$this->moduleCacheService->get($cacheKey)
);
}

Expand All @@ -76,15 +73,14 @@ public function getClassExtensions(): array
$shopId = $this->context->getCurrentShopId();
$cacheKey = 'module_class_extensions';

if (!$this->moduleCacheService->exists($cacheKey, $shopId)) {
if (!$this->moduleCacheService->exists($cacheKey)) {
$this->moduleCacheService->put(
$cacheKey,
$shopId,
$this->activeClassExtensionChainResolver->getActiveExtensionChain($shopId)->getChain()
);
}

return $this->moduleCacheService->get($cacheKey, $shopId);
return $this->moduleCacheService->get($cacheKey);
}

/** @return array */
Expand Down
11 changes: 4 additions & 7 deletions source/Internal/Framework/Module/Facade/ModuleSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
class ModuleSettingService implements ModuleSettingServiceInterface
{
public function __construct(
private ContextInterface $context,
private ModuleConfigurationDaoInterface $moduleConfigurationDao,
private ModuleCacheServiceInterface $moduleCacheService,
private EventDispatcherInterface $eventDispatcher
private readonly ContextInterface $context,
private readonly ModuleConfigurationDaoInterface $moduleConfigurationDao,
private readonly ModuleCacheServiceInterface $moduleCacheService,
private readonly EventDispatcherInterface $eventDispatcher
) {
}

Expand Down Expand Up @@ -92,8 +92,6 @@ private function saveSettingToModuleConfiguration(string $moduleId, string $name
{
$shopId = $this->context->getCurrentShopId();

$this->moduleCacheService->invalidate($moduleId, $shopId);

$moduleConfiguration = $this->moduleConfigurationDao->get($moduleId, $shopId);
$setting = $moduleConfiguration->getModuleSetting($name);
$setting->setValue($value);
Expand All @@ -110,7 +108,6 @@ private function getValue(string $moduleId, string $name): mixed
if (!$this->moduleCacheService->exists($cacheKey, $shopId)) {
$this->moduleCacheService->put(
$cacheKey,
$shopId,
['value' => $this->moduleConfigurationDao->get($moduleId, $shopId)->getModuleSetting($name)->getValue()]
);
}
Expand Down
14 changes: 8 additions & 6 deletions source/Internal/Framework/Theme/Command/ThemeActivateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Theme\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,8 +26,9 @@ class ThemeActivateCommand extends Command

public function __construct(
private readonly ShopAdapterInterface $shopAdapter,
private readonly TemplateCacheServiceInterface $templateCacheService,
private readonly ModuleCacheServiceInterface $moduleCacheService
private readonly ShopTemplateCacheServiceInterface $shopTemplateCacheService,
private readonly ShopPoolServiceInterface $shopPoolService,
private readonly BasicContextInterface $context
) {
parent::__construct();
}
Expand Down Expand Up @@ -57,8 +59,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->shopAdapter->activateTheme($themeId);
$this->moduleCacheService->invalidateAll();
$this->templateCacheService->invalidateTemplateCache();
$this->shopPoolService->invalidate($this->context->getCurrentShopId());
$this->shopTemplateCacheService->invalidateAllShopsCache();
$output->writeLn('<info>' . sprintf(self::MESSAGE_THEME_ACTIVATED, $themeId) . '</info>');

return Command::SUCCESS;
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/Core/ModuleVariablesLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ private function putChainToCache(array $chain): void
->get(ModuleCacheServiceInterface::class)
->put(
$cacheKey,
$shopId,
$chain
);
}
Expand Down
Loading

0 comments on commit 6c55261

Please sign in to comment.