Quicknav Close

MODX Cache Rebuild / Refresh

Decrease the average page load time of your MODX page

If you build large pages with MODX, you eventually encounter slow page load times after changing a template or any other page. Pages with hundreds of subpages seem to load very slowly, because the cache has to be recreated for the affected pages. I have checked numerous plugins that are available for MODX but none did the job easily without changing the documents, snippet calls or the individual caching settings. On the initial page load (first time load after refresh / update), it takes around 3-5 seconds untill it is parsed and displayed. When I hit the same page the second time, it takes less than half a second. However, I don’t have time to manually click through the entire page to make sure all the pages are cached again.

I came up with a solution that is a very simple workaround. Most people generate XML Sitemaps for their webpages that are then submitted to the Google Webmaster Tools for better page indexing. MODX offers a package called: GoogleSiteMap. The package includes a Snippet that outputs all published webpages and iterates through your entire context to generate a XML based page index.

The only thing left to do is to parse this XML file, iterate over all URLS and open them with CURL to initiate the page caching.

Depending on the size of the page it might take some time to rebuild the entire cache. You can either start the script manually or use a CORN job to run it scheduled.

The pages now load much faster.

Update: The new MODX 2.3 has way better cache rebuild functions integrated. Which makes this method obsolete... Nevertheless it could still come in handy in some special cases.

The Script



<?php

$timeStart = microtime(true);

  

function downloadXML($path){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$path);

    curl_setopt($ch, CURLOPT_FAILONERROR,1);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $retValue = curl_exec($ch); 

    curl_close($ch);

    return $retValue;

}

 

function openPage($path){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$path);

    curl_setopt($ch, CURLOPT_FAILONERROR,1);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 15);

    $retValue = curl_exec($ch); 

    curl_close($ch);

    return true;

}

 

$xmlPath = "<strong>YOUR XML URL</strong>";

$googleSitemapXML = downloadXML($xmlPath);

$xmlData = new SimpleXMLElement($googleSitemapXML);

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Rebuild MODX Cache</title>

<style>

#progressBar {

width:800px; height:20px; padding:1px; border:1px solid #000; overflow:hidden;

}

#progressBar div {

    float:left;

    border-left:1px solid #fff;

    height:20px;

    background-color:#F0F;

}

.clear { clear:both; }

  

</style>

</head>

  

<body style="font-family:Courier New, Courier, monospace;">

<div style="margin 0 auto;margin-top:20px;">

<?php

$totalURLS = count($xmlData->children());

$totalTime = $totalURLS*15/60;

$progressWidth = ceil(800 / $totalURLS) - 1;

echo "-----------------------------------------------<br />";

echo "REBUILD CACHE FOR ".$totalURLS." URLS<br />";

echo "Estimated Time: ".round($totalTime,0)." Minutes (be patient, do not refresh)<br />";

echo "-----------------------------------------------<br /><br />";

?>

<div id="progressBar"> 

<?php

foreach( $xmlData->children() AS $url )

    {

    if(openPage($url->loc)) {

        echo "<div style=\"width:".$progressWidth."px;\" title=\"".$url->loc."\"></div>\n";

    }

}

?>

<div class="clear"></div>

</div>

  

<?php

  

$time_end = microtime(true);

$time = $time_end - $time_start;

  

echo "<br />";

echo "Total Time Elapsed: ". $time. " Seconds<br />";

echo "-----------------------------------------------<br />";

echo "© 2013 developed by <a style=\"color:#000;\" href=\"http://www.cloudtec.ch\">cloudtec.ch</a>";

  

?>

  

  

</div>

</body>

</html>

by cloudtec Veröffentlicht: Aktualisiert: 2. August 2017

MODX Cache Rebuild / Refresh cloudtec AG
Sandrainstrasse 17 3007 Bern Switzerland