-
-
Save opengeek/ec3f885cefe17e470aa1 to your computer and use it in GitHub Desktop.
A general purpose script to prime a MODX site cache.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tstart = microtime(true); | |
set_time_limit(0); | |
require 'config.core.php'; | |
require MODX_CORE_PATH . 'model/modx/modx.class.php'; | |
$modx = modX::getInstance(); | |
$modx->initialize('mgr'); | |
$modx->setLogLevel(modX::LOG_LEVEL_INFO); | |
$modx->setLogTarget('ECHO'); | |
$modx->log(modX::LOG_LEVEL_INFO, "Regenerating MODX Cache"); | |
try { | |
$iterator = $modx->getIterator('modContext', array('key:!=' => 'mgr')); | |
foreach ($iterator as $context) { | |
if ($modx->switchContext($context->get('key'))) { | |
$modx->setLogLevel(modX::LOG_LEVEL_INFO); | |
$modx->setLogTarget('ECHO'); | |
$modx->log(modX::LOG_LEVEL_INFO, "Processing Resources in Context {$context->get('key')}"); | |
$map = array_reverse($modx->context->aliasMap, true); | |
$modx->log(modX::LOG_LEVEL_INFO, "Processing aliasMap: " . print_r($map, true)); | |
$curl = curl_init(); | |
foreach ($map as $uri => $id) { | |
$resource = $modx->getObject('modResource', $id); | |
if ($resource && $resource->get('cacheable')) { | |
if (in_array($resource->get('class_key'), array('modDocument', 'modSymLink', 'modStaticResource'))) { | |
$url = $modx->makeUrl($resource->get('id'), '', '', 'full'); | |
if (!empty($url)) { | |
$modx->log(modX::LOG_LEVEL_INFO, "Requesting Resource at {$url}"); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_FAILONERROR, false); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_exec($curl); | |
$modx->log(modX::LOG_LEVEL_INFO, "Updated cache for resource at {$url}"); | |
} | |
} | |
} | |
} | |
curl_close($curl); | |
} else { | |
$modx->log(modX::LOG_LEVEL_ERROR, "RegenCache: Could not switchContext({$context->get('key')})"); | |
} | |
} | |
} catch (Exception $e) { | |
$tend = microtime(true); | |
$totalTime= sprintf("%2.4f seconds", ($tend - $tstart)); | |
$modx->log(modX::LOG_LEVEL_ERROR, "RegenCache failed in {$totalTime}: {$e->getMessage()}"); | |
} | |
$tend = microtime(true); | |
$totalTime= sprintf("%2.4f seconds", ($tend - $tstart)); | |
$modx->log(modX::LOG_LEVEL_INFO, "RegenCache executed in {$totalTime}"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment