Automatic cache clearing, when editing any record
In a recent project I wanted to clear caches for pages, that display certain records, when one of these records was updated. In this case it was courses and the available sessions.
I found out, that TYPO3 already does some automatic cache clearing, when saving or hiding/unhiding records, based on tags build of the table name and the uid, like `{tableName}_{uid}`. So the only thing I needed to was to update my controller code to add cache tags:
class CourseController extends ActionController {
...
public function listAction() {
$cacheTags = [];
// Fetch all courses
$courses = $this->courseRepository->findAll();
// Build cache tags
foreach ($courses as $course) {
$cacheTags[] = 'tx_course_domain_model_course_' . $course->getUid();
}
// Add cache tags to page cache
$this->getTypoScriptFrontendController()->addCacheTags($cacheTags);
$this->view->assign('courses', $courses);
}
...
/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
...
}
This was tested with TYPO3 CMS 10.4.
Dette website bruger Disqus for at vise kommentarer. På grund af databeskyttelsesregler (GDPR) skal du godkende brugen af eksterne tjenester som Disqus.
Vis kommentarer