Skip to content

Commit

Permalink
[BUGFIX] fix not clearing pages
Browse files Browse the repository at this point in the history
- simplify setup
  • Loading branch information
twoldanski committed Jan 27, 2022
1 parent cf989b8 commit 8201b5d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 136 deletions.
158 changes: 26 additions & 132 deletions Classes/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand All @@ -41,70 +43,29 @@ final class CacheManager implements LoggerAwareInterface
* @var array<string>
*/
protected array $clearQueue = [];
/**
* @var array<string>
*/
protected array $clearQueueTags = [];
/**
* @var array<string>
*/
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.'] ?? [];
}

/**
* @param string $tag
*/
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;
}

Expand All @@ -115,29 +76,18 @@ 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;
}
$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
Expand Down Expand Up @@ -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);
Expand All @@ -179,8 +129,6 @@ public function execute(): void
curl_multi_close($multiHandle);

$this->clearQueue = [];
$this->clearQueueTags = [];
$this->clearQueueSoftTags = [];
}

public function __destruct()
Expand All @@ -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;
Expand All @@ -218,60 +158,14 @@ public function clearCache(?string $cmd): void
}
}

/**
* @param array<string> $paths
*/
public function addPathsToQueue(array $paths): void
{
$this->clearQueue = array_merge($this->clearQueue, $paths);
$this->clearQueue = array_unique($this->clearQueue);
}

/**
* @param array<string> $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
* @return false|resource
*/
protected function getCurlHandleForCacheClearingAsTag(string $tag, string $varnishUrl)
{
$this->logger->debug('getCurlHandleForCacheClearing: ' . $tag);

return $this->createCurlHandle($varnishUrl, 'X-Tags: ' . $tag, 'BAN');
}

/**
* @param array<string> $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);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

0 comments on commit 8201b5d

Please sign in to comment.