Skip to content

Commit

Permalink
Updated cron class to follow event/observer logic as per others
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspronko committed May 11, 2023
1 parent dcd4f9c commit 2cad5ac
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Cron/FlushInvalidatedCacheTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
namespace Pronko\SelectiveCache\Cron;

use Psr\Log\LoggerInterface;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\DataObject\Factory;
use Magento\Framework\Event\Manager as EventManager;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Class FlushInvalidatedCacheTypes flushes invalidated cache types by cronjob
*/
class FlushInvalidatedCacheTypes
{
/**
* @var TypeListInterface
*/
private TypeListInterface $cacheTypeList;

/**
* @var ScopeConfigInterface
*/
Expand All @@ -32,16 +28,29 @@ class FlushInvalidatedCacheTypes
private LoggerInterface $logger;

/**
* @param TypeListInterface $cacheTypeList
* @var EventManager
*/
private EventManager $eventManager;

/**
* @var Factory
*/
private Factory $dataObjectFactory;

/**
* @param EventManager $eventManager
* @param Factory $dataObjectFactory
* @param ScopeConfigInterface $scopeConfig
* @param LoggerInterface $logger
*/
public function __construct(
TypeListInterface $cacheTypeList,
EventManager $eventManager,
Factory $dataObjectFactory,
ScopeConfigInterface $scopeConfig,
LoggerInterface $logger
) {
$this->cacheTypeList = $cacheTypeList;
$this->eventManager = $eventManager;
$this->dataObjectFactory = $dataObjectFactory;
$this->scopeConfig = $scopeConfig;
$this->logger = $logger;
}
Expand All @@ -57,15 +66,19 @@ public function execute(): void
return;
}

foreach ($this->cacheTypeList->getInvalidated() as $invalidatedType) {
$this->cacheTypeList->cleanType($invalidatedType->getData('id'));
$cacheLabels[] = $invalidatedType->getData('cache_type');
}
$cacheContainer = $this->dataObjectFactory->create();

$this->eventManager->dispatch(
'cache_flush_invalidated',
['cache_container' => $cacheContainer]
);

$labels = $cacheContainer->getData('labels');

//TODO add configuration setting to enable/disable logging
if (!empty($cacheLabels)) {
if (!empty($labels)) {
$this->logger->info(
sprintf("Cache types cleared automatically: %s", implode(', ', $cacheLabels))
sprintf("Cache types cleared automatically: %s", implode(', ', $labels))
);
}
}
Expand Down

0 comments on commit 2cad5ac

Please sign in to comment.