Created
March 22, 2019 15:01
-
-
Save helhum/0b047c9b91ebc6e6bb52cf934f47b43c to your computer and use it in GitHub Desktop.
TYPO3 Cache Warming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SYS: | |
caching: | |
cacheConfigurations: | |
cache_hash: | |
frontend: 'Helhum\SitePackage\Cache\Frontend\CacheWarmingFrontend' | |
frontendOptions: | |
tokenName: 'warmFrontendCache' | |
token: '%env(TYPO3_CACHE_WARMER_TOKEN)%' | |
cache_menu: | |
frontend: 'Helhum\SitePackage\Cache\Frontend\CacheWarmingFrontend' | |
frontendOptions: | |
tokenName: 'warmMenuCache' | |
token: '%env(TYPO3_CACHE_WARMER_TOKEN)%' | |
backend: 'TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend' | |
cache_pages: | |
frontend: 'Helhum\SitePackage\Cache\Frontend\CacheWarmingFrontend' | |
frontendOptions: | |
tokenName: 'warmFrontendCache' | |
token: '%env(TYPO3_CACHE_WARMER_TOKEN)%' | |
cache_pagesection: | |
frontend: 'Helhum\SitePackage\Cache\Frontend\CacheWarmingFrontend' | |
frontendOptions: | |
tokenName: 'warmFrontendCache' | |
token: '%env(TYPO3_CACHE_WARMER_TOKEN)%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Helhum\SitePackage\Cache\Frontend; | |
use TYPO3\CMS\Core\Cache\Backend\BackendInterface; | |
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; | |
use TYPO3\CMS\Core\Core\Environment; | |
use TYPO3\CMS\Core\Http\ServerRequestFactory; | |
class CacheWarmingFrontend extends VariableFrontend | |
{ | |
/** | |
* @var bool | |
*/ | |
private $shouldWarmCache; | |
public function __construct(string $identifier, BackendInterface $backend) | |
{ | |
parent::__construct($identifier, $backend); | |
$options = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$identifier]['frontendOptions'] ?? []; | |
$token = $options['token'] ?? ''; | |
$tokenName = $options['tokenName'] ?? ''; | |
$this->shouldWarmCache = $this->requestHasValidToken($tokenName, $token); | |
} | |
public function has($entryIdentifier): bool | |
{ | |
if ($this->shouldWarmCache) { | |
return false; | |
} | |
return parent::has($entryIdentifier); | |
} | |
public function get($entryIdentifier) | |
{ | |
if ($this->shouldWarmCache) { | |
return false; | |
} | |
return parent::get($entryIdentifier); | |
} | |
private function requestHasValidToken(string $tokenName, string $token): bool | |
{ | |
if (!$token || Environment::isCli()) { | |
return false; | |
} | |
$request = $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals(); | |
$givenToken = $request->getQueryParams()[$tokenName] ?? null; | |
return $givenToken === $token; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to warm up frontend caches then?
Request https://your.domain/url/?warmFrontendCache=your-valid-token in the browser or via curl
or
use TYPO3 Console command
typo3cms frontend:request 'https://your.domain/url/?warmFrontendCache=your-valid-token'
Have fun!