Skip to content

Commit

Permalink
Fix the cache tag invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Dec 8, 2020
1 parent 0df5442 commit b523f61
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 22 deletions.
18 changes: 13 additions & 5 deletions calendar-bundle/src/Resources/contao/classes/Events.php
Expand Up @@ -283,13 +283,21 @@ protected function addEvent($objEvents, $intStart, $intEnd, $intBegin, $intLimit
}
}

// Tag the response
// Tag the event and its child content elements (see #2137). Calendars
// will be tagged separately in the related front end modules.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
/** @var ResponseTagger $responseTagger */
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvents->id));
$responseTagger->addTags(array('contao.db.tl_calendar.' . $objEvents->pid));
$arrTags = array('contao.db.tl_calendar_events.' . $objEvents->id);

if ($objElements = ContentModel::findPublishedByPidAndTable($objEvents->id, 'tl_calendar_events'))
{
foreach ($objElements as $objElement)
{
$arrTags[] = 'contao.db.tl_content.' . $objElement->id;
}
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

// Store raw data
Expand Down
14 changes: 14 additions & 0 deletions calendar-bundle/src/Resources/contao/modules/ModuleCalendar.php
Expand Up @@ -71,6 +71,20 @@ public function generate()
return '';
}

// Add a response tag for every calendar (see #2137). Events and content
// elements will be tagged automatically in the getAllEvents() method.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$arrTags = array();

foreach ($this->cal_calendar as $id)
{
$arrTags[] = 'contao.db.tl_calendar.' . $id;
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

$this->strUrl = preg_replace('/\?.*$/', '', Environment::get('request'));
$this->strLink = $this->strUrl;

Expand Down
17 changes: 12 additions & 5 deletions calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php
Expand Up @@ -238,13 +238,20 @@ protected function compile()
$objTemplate->hasDetails = false;
$objTemplate->hasTeaser = false;

// Tag the response
// Tag the event and its child content elements (see #2137)
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
/** @var ResponseTagger $responseTagger */
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvent->id));
$responseTagger->addTags(array('contao.db.tl_calendar.' . $objEvent->pid));
$arrTags = array('contao.db.tl_calendar_events.' . $objEvent->id);

if ($objElements = ContentModel::findPublishedByPidAndTable($objEvent->id, 'tl_calendar_events'))
{
foreach ($objElements as $objElement)
{
$arrTags[] = 'contao.db.tl_content.' . $objElement->id;
}
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

// Clean the RTE output
Expand Down
15 changes: 15 additions & 0 deletions calendar-bundle/src/Resources/contao/modules/ModuleEventlist.php
Expand Up @@ -11,6 +11,7 @@
namespace Contao;

use Contao\CoreBundle\Exception\PageNotFoundException;
use FOS\HttpCache\ResponseTagger;
use Patchwork\Utf8;

/**
Expand Down Expand Up @@ -77,6 +78,20 @@ public function generate()
return $this->getFrontendModule($this->cal_readerModule, $this->strColumn);
}

// Add a response tag for every calendar (see #2137). Events and content
// elements will be tagged automatically in the getAllEvents() method.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$arrTags = array();

foreach ($this->cal_calendar as $id)
{
$arrTags[] = 'contao.db.tl_calendar.' . $id;
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

return parent::generate();
}

Expand Down
73 changes: 66 additions & 7 deletions core-bundle/src/Resources/contao/classes/DataContainer.php
Expand Up @@ -1278,14 +1278,10 @@ public function invalidateCacheTags()
return;
}

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

if (!empty($this->ptable) && $this->activeRecord && $this->activeRecord->pid > 0)
{
$tags[] = $ns . $this->ptable;
$tags[] = $ns . $this->ptable . '.' . $this->activeRecord->pid;
}
$this->addPtableTags($this->table, $this->id, $tags);
$this->addCtableTags($this->table, $this->id, $tags);

// Trigger the oninvalidate_cache_tags_callback
if (\is_array($GLOBALS['TL_DCA'][$this->table]['config']['oninvalidate_cache_tags_callback']))
Expand All @@ -1312,6 +1308,69 @@ public function invalidateCacheTags()
$cacheManager->invalidateTags($tags);
}

private function addPtableTags($strTable, $intId, &$tags)
{
if (empty($GLOBALS['TL_DCA'][$strTable]['config']['ptable']))
{
return;
}

$ptable = $GLOBALS['TL_DCA'][$strTable]['config']['ptable'];

Controller::loadDataContainer($ptable);

$objPid = $this->Database->prepare('SELECT pid FROM ' . Database::quoteIdentifier($strTable) . ' WHERE id=?')
->execute($intId);

if (!$objPid->numRows)
{
return;
}

$tags[] = 'contao.db.' . $ptable . '.' . $objPid->pid;

$this->addPtableTags($ptable, $objPid->pid, $tags);
}

private function addCtableTags($strTable, $intId, &$tags)
{
if (empty($GLOBALS['TL_DCA'][$strTable]['config']['ctable']))
{
return;
}

foreach ($GLOBALS['TL_DCA'][$strTable]['config']['ctable'] as $ctable)
{
Controller::loadDataContainer($ctable);

if ($GLOBALS['TL_DCA'][$ctable]['config']['dynamicPtable'])
{
$objIds = $this->Database->prepare('SELECT id FROM ' . Database::quoteIdentifier($ctable) . ' WHERE pid=? AND ptable=?')
->execute($intId, $strTable);
}
else
{
$objIds = $this->Database->prepare('SELECT id FROM ' . Database::quoteIdentifier($ctable) . ' WHERE pid=?')
->execute($intId);
}

if (!$objIds->numRows)
{
continue;
}

while ($objIds->next())
{
$tags[] = 'contao.db.' . $ctable . '.' . $objIds->id;

if (!empty($GLOBALS['TL_DCA'][$ctable]['config']['ctable']))
{
$this->addCtableTags($ctable, $objIds->id, $tags);
}
}
}
}

/**
* Return the name of the current palette
*
Expand Down
18 changes: 13 additions & 5 deletions news-bundle/src/Resources/contao/modules/ModuleNews.php
Expand Up @@ -223,13 +223,21 @@ protected function parseArticle($objArticle, $blnAddArchive=false, $strClass='',
}
}

// Tag the response
// Tag the news and its child content elements (see #2137). News archives
// will be tagged separately in the related front end modules.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
/** @var ResponseTagger $responseTagger */
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array('contao.db.tl_news.' . $objArticle->id));
$responseTagger->addTags(array('contao.db.tl_news_archive.' . $objArticle->pid));
$arrTags = array('contao.db.tl_news.' . $objArticle->id);

if ($objElements = ContentModel::findPublishedByPidAndTable($objArticle->id, 'tl_news'))
{
foreach ($objElements as $objElement)
{
$arrTags[] = 'contao.db.tl_content.' . $objElement->id;
}
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

return $objTemplate->parse();
Expand Down
14 changes: 14 additions & 0 deletions news-bundle/src/Resources/contao/modules/ModuleNewsArchive.php
Expand Up @@ -73,6 +73,20 @@ public function generate()
return '';
}

// Add a response tag for every news archive (see #2137). News and content
// elements will be tagged automatically in the parseArticles() method.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$arrTags = array();

foreach ($this->news_archives as $id)
{
$arrTags[] = 'contao.db.tl_news_archive.' . $id;
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

return parent::generate();
}

Expand Down
14 changes: 14 additions & 0 deletions news-bundle/src/Resources/contao/modules/ModuleNewsList.php
Expand Up @@ -66,6 +66,20 @@ public function generate()
return $this->getFrontendModule($this->news_readerModule, $this->strColumn);
}

// Add a response tag for every news archive (see #2137). News and content
// elements will be tagged automatically in the parseArticles() method.
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$arrTags = array();

foreach ($this->news_archives as $id)
{
$arrTags[] = 'contao.db.tl_news_archive.' . $id;
}

System::getContainer()->get('fos_http_cache.http.symfony_response_tagger')->addTags($arrTags);
}

return parent::generate();
}

Expand Down

0 comments on commit b523f61

Please sign in to comment.