ModifyCacheLifetimeForPageEvent

New in version 12.0: This event serves as a successor for the $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['get_cache_timeout'] hook.

This event allows to modify the lifetime of how long a rendered page of a frontend call should be stored in the “pages” cache.

API

class TYPO3\CMS\Frontend\Event\ModifyCacheLifetimeForPageEvent

Event to allow listeners to modify the amount of seconds that a generated frontend page should be cached in the “pages” cache when initially generated.

setCacheLifetime(int cacheLifetime)
Parameters:
  • $cacheLifetime (int) – the cacheLifetime
getCacheLifetime()
Return type:int
getPageId()
Return type:int
getPageRecord()
Return type:array
getRenderingInstructions()
Return type:array
getContext()
Return type:TYPO3\CMS\Core\Context\Context

Example

Register the listener:

my_extension/Configuration/Services.yaml
services:
  MyCompany\MyPackage\EventListener\ChangeCacheTimeout:
    tags:
      - name: event.listener
        identifier: 'mycompany/myextension/cache-timeout'

The following listener limits the cache lifetime to 30 seconds in development context:

my_extension/EventListener/ChangeCacheTimeout.php
namespace MyCompany\MyExtension\EventListener;
use TYPO3\CMS\Frontend\Event\ModifyCacheLifetimeForPageEvent;

class ChangeCacheTimeout
{
    public function __invoke(ModifyCacheLifetimeForPageEvent $event): void
    {
        // Only cache all pages for 30 seconds when in development context
        if (Environment::getContext()->isDevelopment()) {
            $event->setCacheLifetime(30);
        }
    }
}