/** * Return the current cache instance * * @return DynamicCache */ public static function inst() { if (!self::$instance) { self::$instance = DynamicCache::create(); } return self::$instance; }
public function onBeforeInit() { // Determine if this page is of a non-cacheable type $ignoredClasses = DynamicCache::config()->ignoredPages; $ignoredByClass = false; if ($ignoredClasses) { foreach ($ignoredClasses as $ignoredClass) { if (is_a($this->owner->data(), $ignoredClass, true)) { $ignoredByClass = true; break; } } } $isStage = ($stage = Versioned::current_stage()) && $stage !== 'Live'; // Set header disabling caching if // - current page is an ignored page type // - current_stage is not live if ($ignoredByClass || $isStage) { $header = DynamicCache::config()->optOutHeaderString; header($header); } // Flush cache if requested if (isset($_GET['flush']) || isset($_GET['cache']) && $_GET['cache'] === 'flush' && Permission::check('ADMIN')) { DynamicCache::inst()->clear(); } }
public function onBeforeInit() { // If not on live site, set header disabling caching to prevent caching of draft content if (($stage = Versioned::current_stage()) && $stage !== 'Live') { $header = DynamicCache::config()->optOutHeaderString; header($header); } }
/** * Clear the entire dynamic cache once a dataobject has been deleted. * Safe and dirty. * */ public function onBeforeDelete() { DynamicCache::inst()->clear(); }
ini_set('error_log', Director::baseFolder() . '/error.log'); SS_Log::add_writer(new SS_LogFileWriter(Director::baseFolder() . '/silverstripe.log'), SS_Log::INFO, '<='); // Set a cache (disabled in dev mode anyway) //HTTP::set_cache_age(60 * 30); // 30 min //Might be better to add this to "Page::init" // Configure according to environment if (Director::isDev()) { // Display all errors error_reporting(-1); // Add a debug logger SS_Log::add_writer(new SS_LogFileWriter(Director::baseFolder() . '/debug.log'), SS_Log::DEBUG, '='); // Send emails to admin Email::send_all_emails_to(Email::config()->admin_email); // Disable DynamicCache if (class_exists('DynamicCache')) { DynamicCache::config()->enabled = false; } // See where are included files Config::inst()->update('SSViewer', 'source_file_comments', true); // Fix this issue https://github.com/silverstripe/silverstripe-framework/issues/4146 if (isset($_GET['flush'])) { i18n::get_cache()->clean(Zend_Cache::CLEANING_MODE_ALL); } } else { // In production, sanitize php environment to avoid leaking information ini_set('display_errors', false); // Hide where are included files Config::inst()->update('SSViewer', 'source_file_comments', false); // Warn admin if errors occur SS_Log::add_writer(new SS_LogEmailWriter(Email::config()->admin_email), SS_Log::ERR, '<='); }
$i = strpos($url, '?'); if ($i !== false) { $url = substr($url, 0, $i); } // Lighttpd uses this } else { if (strpos($_SERVER['REQUEST_URI'], '?') !== false) { list($url, $query) = explode('?', $_SERVER['REQUEST_URI'], 2); parse_str($query, $_GET); if ($_GET) { $_REQUEST = array_merge((array) $_REQUEST, (array) $_GET); } } else { $url = $_SERVER["REQUEST_URI"]; } } } // Remove base folders from the URL if webroot is hosted in a subfolder if (strlen($url) && strlen(BASE_URL)) { if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) { $url = substr($url, strlen(BASE_URL)); } } if (empty($url)) { $url = '/'; } elseif (substr($url, 0, 1) !== '/') { $url = "/{$url}"; } // Activate caching here $instance = DynamicCache::inst(); $instance->run($url);
/** * 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); }