Skip to content

Commit 103649e

Browse files
authored
Fix the cache tag invalidation (see contao#2551)
Description ----------- | Q | A | -----------------| --- | Fixed issues | Fixes contao#2137 | Docs PR or issue | - This is currently a proof of concept that should work for news and events. @Toflar @ausi Can you confirm that my implementation is what you had in mind? ### TODO * [x] Add a general tag without ID for the top parent record * [x] Implement cache tagging for the other elements and modules Commits ------- b523f61 Fix the cache tag invalidation 64c4cdc CS 4b61ac2 Do not double tag the content elements 5b4a03d Add a tag for the top parent element 996694e Add tagging for the missing modules 601247f Also tag the comments
1 parent 137d410 commit 103649e

File tree

19 files changed

+184
-46
lines changed

19 files changed

+184
-46
lines changed

calendar-bundle/src/Resources/contao/classes/Events.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace Contao;
1212

13-
use FOS\HttpCache\ResponseTagger;
14-
1513
/**
1614
* Provide methods to get all events of a certain period from the database.
1715
*
@@ -283,13 +281,11 @@ protected function addEvent($objEvents, $intStart, $intEnd, $intBegin, $intLimit
283281
}
284282
}
285283

286-
// Tag the response
284+
// Tag the event (see #2137)
287285
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
288286
{
289-
/** @var ResponseTagger $responseTagger */
290287
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
291288
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvents->id));
292-
$responseTagger->addTags(array('contao.db.tl_calendar.' . $objEvents->pid));
293289
}
294290

295291
// Store raw data

calendar-bundle/src/Resources/contao/modules/ModuleCalendar.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function generate()
8080
$this->strLink = $objTarget->getFrontendUrl();
8181
}
8282

83+
// Tag the calendars (see #2137)
84+
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
85+
{
86+
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
87+
$responseTagger->addTags(array_map(static function ($id) { return 'contao.db.tl_calendar.' . $id; }, $this->cal_calendar));
88+
}
89+
8390
return parent::generate();
8491
}
8592

calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Contao\CoreBundle\Exception\InternalServerErrorException;
1414
use Contao\CoreBundle\Exception\PageNotFoundException;
1515
use Contao\CoreBundle\Exception\RedirectResponseException;
16-
use FOS\HttpCache\ResponseTagger;
1716
use Patchwork\Utf8;
1817

1918
/**
@@ -238,15 +237,6 @@ protected function compile()
238237
$objTemplate->hasDetails = false;
239238
$objTemplate->hasTeaser = false;
240239

241-
// Tag the response
242-
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
243-
{
244-
/** @var ResponseTagger $responseTagger */
245-
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
246-
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvent->id));
247-
$responseTagger->addTags(array('contao.db.tl_calendar.' . $objEvent->pid));
248-
}
249-
250240
// Clean the RTE output
251241
if ($objEvent->teaser)
252242
{
@@ -402,6 +392,13 @@ protected function compile()
402392

403393
$this->Template->event = $objTemplate->parse();
404394

395+
// Tag the event (see #2137)
396+
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
397+
{
398+
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
399+
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvent->id));
400+
}
401+
405402
$bundles = System::getContainer()->getParameter('kernel.bundles');
406403

407404
// HOOK: comments extension required

calendar-bundle/src/Resources/contao/modules/ModuleEventlist.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function generate()
7777
return $this->getFrontendModule($this->cal_readerModule, $this->strColumn);
7878
}
7979

80+
// Tag the calendars (see #2137)
81+
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
82+
{
83+
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
84+
$responseTagger->addTags(array_map(static function ($id) { return 'contao.db.tl_calendar.' . $id; }, $this->cal_calendar));
85+
}
86+
8087
return parent::generate();
8188
}
8289

comments-bundle/src/Resources/contao/classes/Comments.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Contao\CoreBundle\Exception\PageNotFoundException;
1414
use Contao\CoreBundle\OptIn\OptIn;
15-
use FOS\HttpCache\ResponseTagger;
1615

1716
/**
1817
* Class Comments
@@ -43,10 +42,9 @@ public function addCommentsToTemplate(FrontendTemplate $objTemplate, \stdClass $
4342

4443
$objTemplate->comments = array(); // see #4064
4544

46-
// Tag the response
45+
// Tag the source record (see #2137)
4746
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
4847
{
49-
/** @var ResponseTagger $responseTagger */
5048
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
5149
$responseTagger->addTags(array(sprintf('contao.comments.%s.%s', $strSource, $intParent)));
5250
}
@@ -102,6 +100,7 @@ public function addCommentsToTemplate(FrontendTemplate $objTemplate, \stdClass $
102100
if ($objComments !== null && ($total = $objComments->count()) > 0)
103101
{
104102
$count = 0;
103+
$tags = array();
105104
$objPartial = new FrontendTemplate($objConfig->template ?: 'com_default');
106105

107106
while ($objComments->next())
@@ -134,8 +133,17 @@ public function addCommentsToTemplate(FrontendTemplate $objTemplate, \stdClass $
134133
}
135134

136135
$arrComments[] = $objPartial->parse();
136+
$tags[] = 'contao.db.tl_comments.' . $objComments->id;
137+
137138
++$count;
138139
}
140+
141+
// Tag the comments (see #2137)
142+
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
143+
{
144+
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
145+
$responseTagger->addTags($tags);
146+
}
139147
}
140148

141149
$objTemplate->comments = $arrComments;

core-bundle/src/Resources/contao/classes/DataContainer.php

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,14 +1278,10 @@ public function invalidateCacheTags()
12781278
return;
12791279
}
12801280

1281-
$ns = 'contao.db.';
1282-
$tags = array($ns . $this->table, $ns . $this->table . '.' . $this->id);
1281+
$tags = array('contao.db.' . $this->table . '.' . $this->id);
12831282

1284-
if (!empty($this->ptable) && $this->activeRecord && $this->activeRecord->pid > 0)
1285-
{
1286-
$tags[] = $ns . $this->ptable;
1287-
$tags[] = $ns . $this->ptable . '.' . $this->activeRecord->pid;
1288-
}
1283+
$this->addPtableTags($this->table, $this->id, $tags);
1284+
$this->addCtableTags($this->table, $this->id, $tags);
12891285

12901286
// Trigger the oninvalidate_cache_tags_callback
12911287
if (\is_array($GLOBALS['TL_DCA'][$this->table]['config']['oninvalidate_cache_tags_callback']))
@@ -1312,6 +1308,68 @@ public function invalidateCacheTags()
13121308
$cacheManager->invalidateTags($tags);
13131309
}
13141310

1311+
private function addPtableTags($strTable, $intId, &$tags)
1312+
{
1313+
if (empty($GLOBALS['TL_DCA'][$strTable]['config']['ptable']))
1314+
{
1315+
$tags[] = 'contao.db.' . $strTable;
1316+
1317+
return;
1318+
}
1319+
1320+
$ptable = $GLOBALS['TL_DCA'][$strTable]['config']['ptable'];
1321+
1322+
Controller::loadDataContainer($ptable);
1323+
1324+
$objPid = $this->Database->prepare('SELECT pid FROM ' . Database::quoteIdentifier($strTable) . ' WHERE id=?')
1325+
->execute($intId);
1326+
1327+
if (!$objPid->numRows)
1328+
{
1329+
return;
1330+
}
1331+
1332+
$tags[] = 'contao.db.' . $ptable . '.' . $objPid->pid;
1333+
1334+
$this->addPtableTags($ptable, $objPid->pid, $tags);
1335+
}
1336+
1337+
private function addCtableTags($strTable, $intId, &$tags)
1338+
{
1339+
if (empty($GLOBALS['TL_DCA'][$strTable]['config']['ctable']))
1340+
{
1341+
return;
1342+
}
1343+
1344+
foreach ($GLOBALS['TL_DCA'][$strTable]['config']['ctable'] as $ctable)
1345+
{
1346+
Controller::loadDataContainer($ctable);
1347+
1348+
if ($GLOBALS['TL_DCA'][$ctable]['config']['dynamicPtable'])
1349+
{
1350+
$objIds = $this->Database->prepare('SELECT id FROM ' . Database::quoteIdentifier($ctable) . ' WHERE pid=? AND ptable=?')
1351+
->execute($intId, $strTable);
1352+
}
1353+
else
1354+
{
1355+
$objIds = $this->Database->prepare('SELECT id FROM ' . Database::quoteIdentifier($ctable) . ' WHERE pid=?')
1356+
->execute($intId);
1357+
}
1358+
1359+
if (!$objIds->numRows)
1360+
{
1361+
continue;
1362+
}
1363+
1364+
while ($objIds->next())
1365+
{
1366+
$tags[] = 'contao.db.' . $ctable . '.' . $objIds->id;
1367+
1368+
$this->addCtableTags($ctable, $objIds->id, $tags);
1369+
}
1370+
}
1371+
}
1372+
13151373
/**
13161374
* Return the name of the current palette
13171375
*

core-bundle/src/Resources/contao/classes/FrontendTemplate.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Contao;
1212

13-
use FOS\HttpCache\ResponseTagger;
1413
use Symfony\Component\HttpFoundation\Response;
1514

1615
/**
@@ -403,10 +402,9 @@ private function setCacheHeaders(Response $response)
403402
$response->setVary(array('Cookie'));
404403
}
405404

406-
// Tag the response with cache tags für the shared cache only
405+
// Tag the page (see #2137)
407406
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
408407
{
409-
/** @var ResponseTagger $responseTagger */
410408
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
411409
$responseTagger->addTags(array('contao.db.tl_page.' . $objPage->id));
412410
}

core-bundle/src/Resources/contao/elements/ContentElement.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace Contao;
1212

1313
use Contao\Model\Collection;
14-
use FOS\HttpCache\ResponseTagger;
1514

1615
/**
1716
* Parent class for content elements.
@@ -274,10 +273,9 @@ public function generate()
274273
$this->Template->class .= ' ' . implode(' ', $this->objModel->classes);
275274
}
276275

277-
// Tag the response
276+
// Tag the content element (see #2137)
278277
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
279278
{
280-
/** @var ResponseTagger $responseTagger */
281279
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
282280
$responseTagger->addTags(array('contao.db.tl_content.' . $this->id));
283281
}

core-bundle/src/Resources/contao/elements/ContentModule.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace Contao;
1212

13-
use FOS\HttpCache\ResponseTagger;
14-
1513
/**
1614
* Front end content element "module".
1715
*
@@ -67,10 +65,9 @@ public function generate()
6765
/** @var Module $objModule */
6866
$objModule = new $strClass($objModel, $this->strColumn);
6967

70-
// Tag the response
68+
// Tag the content element (see #2137)
7169
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
7270
{
73-
/** @var ResponseTagger $responseTagger */
7471
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
7572
$responseTagger->addTags(array('contao.db.tl_content.' . $this->id));
7673
}

core-bundle/src/Resources/contao/modules/Module.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace Contao;
1212

1313
use Contao\Model\Collection;
14-
use FOS\HttpCache\ResponseTagger;
1514

1615
/**
1716
* Parent class for front end modules.
@@ -236,10 +235,9 @@ public function generate()
236235
$this->Template->class .= ' ' . implode(' ', $this->objModel->classes);
237236
}
238237

239-
// Tag the response
238+
// Tag the module (see #2137)
240239
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
241240
{
242-
/** @var ResponseTagger $responseTagger */
243241
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
244242
$responseTagger->addTags(array('contao.db.tl_module.' . $this->id));
245243
}

0 commit comments

Comments
 (0)