Skip to content

Instantly share code, notes, and snippets.

@helhum
Created March 22, 2019 15:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helhum/0b047c9b91ebc6e6bb52cf934f47b43c to your computer and use it in GitHub Desktop.
Save helhum/0b047c9b91ebc6e6bb52cf934f47b43c to your computer and use it in GitHub Desktop.
TYPO3 Cache Warming
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)%'
<?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;
}
}
@helhum
Copy link
Author

helhum commented Mar 22, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment