diff --git a/Classes/CacheManager.php b/Classes/CacheManager.php index 99b0f35..bfdf5ae 100644 --- a/Classes/CacheManager.php +++ b/Classes/CacheManager.php @@ -4,35 +4,37 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; -use Psr\Log\LoggerInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; -use TYPO3\CMS\Extbase\Object\ObjectManager; - -use function array_merge; use function array_unique; -use function count; use function curl_close; use function curl_errno; use function curl_error; use function curl_getinfo; +use function curl_init; use function curl_multi_add_handle; use function curl_multi_close; +use function curl_multi_exec; use function curl_multi_init; use function curl_multi_remove_handle; +use function curl_multi_select; use function curl_setopt; -use function implode; use function is_array; +use function is_resource; +use const CURLM_CALL_MULTI_PERFORM; +use const CURLM_OK; +use const CURLOPT_CUSTOMREQUEST; +use const CURLOPT_HTTPHEADER; +use const CURLOPT_RETURNTRANSFER; +use const CURLOPT_SSL_VERIFYHOST; +use const CURLOPT_SSL_VERIFYPEER; +use const CURLOPT_URL; final class CacheManager implements LoggerAwareInterface { use LoggerAwareTrait; - /** - * @var LoggerInterface - */ - protected $logger; - /** * @var array */ @@ -41,33 +43,13 @@ final class CacheManager implements LoggerAwareInterface * @var array */ protected array $clearQueue = []; - /** - * @var array - */ - protected array $clearQueueTags = []; - /** - * @var array - */ - protected array $clearQueueSoftTags = []; public function __construct() { - $objectManager = GeneralUtility::makeInstance(ObjectManager::class); - $configurationManager = $objectManager->get(ConfigurationManagerInterface::class); + $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); $this->settings = $configurationManager->getConfiguration( - ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT - )['tx_cachepurger.']['settings.'] ?? []; - } - - /** - * @param string $path - */ - public function clearForUrl(string $path): void - { - $this->logger->debug('clearCacheForUrl: ' . $path); - - $this->clearQueue[] = $path; - $this->clearQueue = array_unique($this->clearQueue); + ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT + )['tx_cachepurger.']['settings.'] ?? []; } /** @@ -75,36 +57,15 @@ public function clearForUrl(string $path): void */ public function clearForTag(string $tag): void { - $this->logger->debug('clearCacheForUrl: ' . $tag); - - $this->clearQueueTags[] = $tag; - $this->clearQueueTags = array_unique($this->clearQueueTags); - } - - /** - * @param string $tag - */ - public function clearSoftForTag(string $tag): void - { - $this->logger->debug('clearCacheSoftForTag: ' . $tag); - - $this->clearQueueSoftTags[] = $tag; - $this->clearQueueSoftTags = array_unique($this->clearQueueSoftTags); + $this->clearQueue[] = $tag; + $this->clearQueue = array_unique($this->clearQueue); } public function execute(): void { $curlHandles = []; - $this->logger->debug('execute: ', $this->clearQueue); - if ( - !is_array($this->settings['domains.']) || - !is_array($this->settings['varnish.']) || - ( - count($this->clearQueue) === 0 || - count($this->clearQueueSoftTags) === 0 - ) - ) { + if (!isset($this->settings['varnish.']) || !is_array($this->settings['varnish.'])) { return; } @@ -115,15 +76,7 @@ public function execute(): void } foreach ($this->settings['varnish.'] as $varnishInstance) { - foreach ($this->clearQueue as $path) { - $ch = $this->getCurlHandleForCacheClearing($path, $varnishInstance); - if (!is_resource($ch)) { - continue; - } - $curlHandles[] = $ch; - curl_multi_add_handle($multiHandle, $ch); - } - foreach ($this->clearQueueTags as $tag) { + foreach ($this->clearQueue as $tag) { $ch = $this->getCurlHandleForCacheClearingAsTag($tag, $varnishInstance); if (!is_resource($ch)) { continue; @@ -131,13 +84,10 @@ public function execute(): void $curlHandles[] = $ch; curl_multi_add_handle($multiHandle, $ch); } + } - $ch = $this->getCurlHandleForSoftCacheClearingAsTag($this->clearQueueSoftTags, $varnishInstance); - if (!is_resource($ch)) { - continue; - } - $curlHandles[] = $ch; - curl_multi_add_handle($multiHandle, $ch); + if (count($curlHandles) === 0) { + return; } // initialize all connections @@ -170,7 +120,7 @@ public function execute(): void $this->logger->error('error: ' . curl_error($ch)); } else { $info = curl_getinfo($ch); - $this->logger->debug('info: ', $info); + $this->logger->error('info: ', $info); } curl_multi_remove_handle($multiHandle, $ch); curl_close($ch); @@ -179,8 +129,6 @@ public function execute(): void curl_multi_close($multiHandle); $this->clearQueue = []; - $this->clearQueueTags = []; - $this->clearQueueSoftTags = []; } public function __destruct() @@ -197,17 +145,9 @@ public function clearCache(?string $cmd): void case 'all': $this->logger->debug('clearCacheCmd() all'); - if (isset($this->settings['domain.'])) { - $this->clearForUrl($this->settings['domain.']); - } - if (isset($this->settings['domains.']) && is_array($this->settings['domains.'])) { - foreach ($this->settings['domains.'] as $basePath) { - $this->clearForUrl($basePath); - } - } if (isset($this->settings['tags.']) && is_array($this->settings['tags.'])) { foreach ($this->settings['tags.'] as $tag) { - $this->clearSoftForTag($tag); + $this->clearForTag($tag); } } break; @@ -218,34 +158,6 @@ public function clearCache(?string $cmd): void } } - /** - * @param array $paths - */ - public function addPathsToQueue(array $paths): void - { - $this->clearQueue = array_merge($this->clearQueue, $paths); - $this->clearQueue = array_unique($this->clearQueue); - } - - /** - * @param array $tags - */ - public function addTagsToQueue(array $tags): void - { - $this->clearQueueTags = array_merge($this->clearQueueTags, $tags); - $this->clearQueueTags = array_unique($this->clearQueueTags); - } - - /** - * @return false|resource - */ - protected function getCurlHandleForCacheClearing(string $url, string $varnishUrl) - { - $this->logger->debug('getCurlHandleForCacheClearing: ' . $url); - - return $this->createCurlHandle($varnishUrl, 'X-Url: (' . $url . ')'); - } - /** * @param string $tag * @param string $varnishUrl @@ -253,25 +165,7 @@ protected function getCurlHandleForCacheClearing(string $url, string $varnishUrl */ protected function getCurlHandleForCacheClearingAsTag(string $tag, string $varnishUrl) { - $this->logger->debug('getCurlHandleForCacheClearing: ' . $tag); - - return $this->createCurlHandle($varnishUrl, 'X-Tags: ' . $tag, 'BAN'); - } - - /** - * @param array $tags - * @param string $varnishUrl - * @return false|resource - */ - protected function getCurlHandleForSoftCacheClearingAsTag(array $tags, string $varnishUrl) - { - $combinedTags = implode(' ', $tags); - - $this->logger->debug('getCurlHandleForCacheClearing: ' . $combinedTags); - - $header = 'X-Tags: ' . $combinedTags; - - return $this->createCurlHandle($varnishUrl, $header, 'BAN'); + return $this->createCurlHandle($varnishUrl, 'X-Tags: ' . $tag); } /** diff --git a/README.md b/README.md index 8fd292b..d43df9a 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,6 @@ tx_cachepurger.settings { 1 = varnish_ip_frontend 2 = varnish_ip_backend } - domains { - 1 = https://frontend_url.tld - 2 = https://backend_url.tld - } tags.0 = T3 } ```