Building a TYPO3 extension in Extbase/Fluid

Automatic page cache clearance on extension record update

Simple version

Especially if you have only one domain model, this version will be fine. The TYPO3 core automatically clears all page caches that are tagged with tx_tablename or tx_tablename_recordUid. Strange thing about it: you’ll have to make your plugins add these tags to the caches as this is not done by the TYPO3 core.

File paths are relative to the root of your extension /typo3conf/ext/your_extension/

add the following variable and method to your Controller /Classes/Controller/YourModelController.php

/**
 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
 */

protected $typoScriptFrontendController;

public function addCacheTags($cacheTag) {
        // do this only in frontend
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
                // only set the tag once in one request, so cache statically if it has been done
                static $cacheTagsSet = FALSE;

                /** @var $typoScriptFrontendController \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController  */
                $typoScriptFrontendController = $GLOBALS['TSFE'];
                if (!$cacheTagsSet) {
                        $typoScriptFrontendController->addCacheTags(array($cacheTag));
                        $cacheTagsSet = TRUE;
                }
                $this->typoScriptFrontendController = $typoScriptFrontendController;
        }
}
        }
}

call this method in your controller’s listAction()

$this->addCacheTags('tx_yourextension_domain_model_yourmodel'); // the database table name of your domain model

call this method in your controller’s showAction(\Vendor\YourExtension\Domain\Model\YourModel $your_model) or detailAction() or whatever you called it

$this->addCacheTags('tx_yourextension_domain_model_yourmodel_'.$your_model->getUid()); // the database table name of your domain model plus the UID of your record

More complex version

This might be useful if you have multiple domain models and let your model controllers return certain properties of another model. You then might want to add the same tag to all pages containing records of any model. Since TYPO3 by itself only flushes caches by the tags tx_tablename and tx_tablename_recordUid you must tell your extension to clear caches by a specific tag.

add the following variable and method to your Controller /Classes/Controller/YourModelController.php

/**
 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
 */

protected $typoScriptFrontendController;

/**
 * Initializes the current action
 *
 * @return void
 */

public function initializeAction() {
        // do this only in frontend
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
                // we only want to set the tag once in one request, so we have to cache that statically if it has been done
                static $cacheTagsSet = FALSE;

                /** @var $typoScriptFrontendController \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController  */
                $typoScriptFrontendController = $GLOBALS['TSFE'];
                if (!$cacheTagsSet) {
                        $typoScriptFrontendController->addCacheTags(array('tx_yourextension'));
                        $cacheTagsSet = TRUE;
                }
                $this->typoScriptFrontendController = $typoScriptFrontendController;
        }
}
        }
}

The initializeAction() method will be called by the TYPO3 core on object instantiation.

For clearing the page caches on editing a record in the backend, add the following line to the page TS Config of the Storage Folder Page that contains the records. You can find it in Page Properties / Resources / TypoScript Configuration

TCEMAIN.clearCacheCmd = cacheTag:tx_yourextension

For clearing the page caches on editing a record in the frontend, add the following file to your extension: /Classes/Hooks/tcemain.php

<?php
namespace Vendor\YourExtension\Hooks;

class Tcemain {

        /**
         * Flushes the cache if an extension record was edited.
         * ONLY WORKS IN FRONTEND EDITING!
         *
         * @return void
         */

        public function clearCachePostProc(array $params) {
                /** @var $cacheManager \TYPO3\CMS\Core\Cache\CacheManager */
                $cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
                $cacheManager->getCache('cache_pages')->flushByTag('tx_yourextension');
                $cacheManager->getCache('cache_pagesection')->flushByTag('tx_yourextension');
        }
}
?>
        }
}
?>

Call this method at the end of your /ext_localconf.php:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'][$_EXTKEY.'_clearcache'] =
        'Vendor\YourExtension\Hooks\Tcemain->clearCachePostProc';
        'Vendor\YourExtension\Hooks\Tcemain->clearCachePostProc';