/** * NIET aanroepen vanuit iets anders dan een CMS functie omdat je anders via cache-main.php en de cURL requests een loop kan opstarten */ public static function rebuildCache() { // Svs: /** * Output is al geflusht in run() dus we kunnen hier naar hartelust nieuwe cachebestanden opbouwen e.d. * Vraag menu op en eventueel extra pagina's. Check of we de url al in de cache hebben. * De eerste 5 pagina's die we niet in de cache hebben gaan we via multithreaded curl requests aanroepen * om zo sneller de hele site weer rap te krijgen. */ $oDynCache = DynamicCache::inst(); $sHostId = Config::inst()->get('HmkMain', 'HostId'); if ($sHostId == 'LOC') { $bIsOnline = false; } else { $bIsOnline = true; } $nMaxCurlCalls = 15; $sHost = $_SERVER['HTTP_HOST']; if ($bIsOnline) { $sBaseUrl = $sHost . '/'; } else { // local $sBaseUrl = $sHost . '/fietsenvoor.nl/'; } $aUrlsToCache = array(); /* $aUrlsToCache[] = ''; // home $aUrlsToCache[] = 'the-blind-run/'; $aUrlsToCache[] = 'the-blind-run/de-deelnemers/'; */ // url al in cache? $aUrlsToCall = array(); foreach ($aUrlsToCache as $sUrlToCache) { $sUrlToCache = 'http://' . $sBaseUrl . $sUrlToCache; // DynamicCacheDataObjectExtension::writeToLog($sUrlToCache); //var_dump(sizeof($aUrlsToCall)); if (sizeof($aUrlsToCall) <= $nMaxCurlCalls) { $cache = $oDynCache->getCache(); $sCacheKeySeed = $sUrlToCache; /* Todo: kijken of er nog iets moet gebeuren met IE cache en meegeven client aan curl request if ($iPosIE = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { $iVersion = substr($_SERVER['HTTP_USER_AGENT'], $iPosIE + 5, 3); $sCacheKeySeed .= 'IE' . $iVersion; } */ $cacheKey = $oDynCache->getCacheKey($sCacheKeySeed); // DynamicCacheDataObjectExtension::writeToLog($sCacheKeySeed); // DynamicCacheDataObjectExtension::writeToLog($cacheKey); DynamicCache::$sUsedCacheKey = $sCacheKeySeed; // Check if cached value can be returned $cachedValue = $cache->load($cacheKey); //var_dump($cachedValue); if ($cachedValue === false) { $aUrlsToCall[] = $sUrlToCache; } } } DynamicCacheDataObjectExtension::writeToLog(print_r($aUrlsToCall, true) . ' --- '); // Loop through the URLs, create curl-handles // and attach the handles to our multi-request foreach ($aUrlsToCall as $url) { $command = 'wget -qO- ' . $url; exec('nohup ' . $command . ' > /dev/null 2>&1 &'); } }
/** * Activate caching on a given url * * @param string $url */ public function run($url) { // Get cache and cache details $responseHeader = self::config()->responseHeader; $cache = $this->getCache(); // Start hamaka custom - geef IE zijn eigen cachefiles omdat anders de combined files van IE in Chrome doorkomen als een IE bezoeker de cache aanmaakt // deze 2 regels kunnen in Page.ss om DynamicCache te debuggen: // <meta name="gen-date" content="{$Now.Nice}" /> // <meta name="dyn-cache" content="{$UsedDynamicCacheKey}" /> $sCacheKeySeed = $url; if ($iPosIE = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { $iVersion = substr($_SERVER['HTTP_USER_AGENT'], $iPosIE + 5, 3); $sCacheKeySeed .= 'IE' . $iVersion; } $cacheKey = $this->getCacheKey($sCacheKeySeed); DynamicCache::$sUsedCacheKey = $sCacheKeySeed; // End hamaka custom // Clear cache if flush = cache or all $this->checkCacheCommands($cache); // Disable CSRF - It doesn't work with cached security tokens shared across sessions SecurityToken::disable(); // Check if caching should be short circuted $enabled = $this->enabled($url); $this->extend('updateEnabled', $enabled); if (!$enabled) { if ($responseHeader) { header("{$responseHeader}: skipped"); } $this->yieldControl(); return; } // Check if cached value can be returned $cachedValue = $cache->load($cacheKey); if ($this->presentCachedResult($cachedValue)) { return; } // Run this page, caching output and capturing data if ($responseHeader) { header("{$responseHeader}: miss at " . @date('r')); } ob_start(); $this->yieldControl(); $headers = headers_list(); $result = ob_get_flush(); // Skip blank copy if (empty($result)) { return; } // Check if any headers match the specified rules forbidding caching if (!$this->headersAllowCaching($headers)) { return; } // Include any "X-Header" sent with this request. This is necessary to // ensure that additional CSS, JS, and other files are retained $saveHeaders = $this->getCacheableHeaders($headers); // Save data along with sent headers $this->cacheResult($cache, $result, $saveHeaders, $cacheKey); }