Discussion:
[TYPO3-english] Extbase/Fluid extension: clear caches for all pages containing a plugin on records data update?
Hagen Gebauer
2015-03-31 12:21:28 UTC
Permalink
Hi everybody,

I am looking for a way to automatically clear the caches of all pages containing a plugin of my calendar extension every time I update the calendar data records. I tried to implement what I think does the job in tx_news, but it does not work. I must have missed something obviously.

/mycalendar/ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'][$_EXTKEY.'_clearcache'] =
'Vendor\Mycalendar\Utility\Tcemain->clearCachePostProc';

/mycalendar/Classes/Utility/Tcemain.php:
<?php
namespace Vendor\Mycalendar\Utility\;

class Tcemain {
/**
* Flushes the cache if a calendar record was edited.
*
* @param array $params
* @return void
*/
public function clearCachePostProc(array $params) {
if(isset($params['table']) && isset($params['uid'])) {
$cacheTag = $params['table'].'_'.$params['uid'];

/** @var $cacheManager \TYPO3\CMS\Core\Cache\CacheManager */
$cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
$cacheManager->getCache('cache_pages')->flushByTag($cacheTag);
$cacheManager->getCache('cache_pagesection')->flushByTag($cacheTag);
$cacheManager->getCache('cache_pages')->flushByTag('tx_mycalendar');
$cacheManager->getCache('cache_pagesection')->flushByTag('tx_mycalendar');
}
}
}
?>

The class method is being called, it obviously also gets the $params array correctly (if I put a php error somewhere into the method it gets reported).

Thanks a lot in advance!

Cheers,
Hagen.
Viktor Livakivskyi
2015-04-01 10:11:22 UTC
Permalink
Hi, Hagen.

There is another possible solution to your problem.

Since TYPO3 6.x (?) core itslef clears the cache by tags 'tx_tablename', 'tx_tablename_recordUid' every time when a change is made on a record from 'tx_tablename' [1]. And it is possible to add page tags from within your own plugins.

So, somewhere in your plugin lisAtcion() (or any other action) you may do following:
/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];
$typoScriptFrontendController->addCacheTags(array('tx_smiproducts_domain_model_device'));

And in your singleViewAction($entity) you may do it like this:
/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];
$typoScriptFrontendController->addCacheTags(array('tx_smiproducts_domain_model_device_' . $entity->getLocalizedUid()));

The rest (cache clearing itself) will be done by core.

[1]: https://forum.typo3.org/index.php?t=tree&th=206708&goto=721412&#msg_721412
Hagen Gebauer
2015-04-01 11:09:23 UTC
Permalink
Hi Viktor,

thank you sooo much!

Cheers,
Hagen.
Hagen Gebauer
2015-04-01 12:26:50 UTC
Permalink
Hello again,

one more thing: I received an error that getLocalizedUid was an unknown method. So in my
showAction(\Vendor\MyExtension\Domain\Model\MyModel $entity) (or singleViewAction) method I had to call it like
$typoScriptFrontendController->addCacheTags(array('tx_myextension_domain_model_mymodel_' . $entity->getUid()));

Cheers,
Hagen.
Viktor Livakivskyi
2015-04-01 12:58:16 UTC
Permalink
Hi, Hagen
Post by Hagen Gebauer
one more thing: I received an error that getLocalizedUid was an unknown method. So in my
showAction(\Vendor\MyExtension\Domain\Model\MyModel $entity) (or singleViewAction) method I had to call it like
$typoScriptFrontendController->addCacheTags(array('tx_myextension_domain_model_mymodel_' . $entity->getUid()));
You're right. I just copy-pasted it from my code and ofrgot to clean-out non-existing methods. But you've got the idea :)
Hagen Gebauer
2015-04-01 14:14:17 UTC
Permalink
Hi Viktor,

I absolutely got the idea! I just wanted to post the correction in case somebody else will need this someday :)
Thanks again!

Cheers,
Hagen.

Loading...