/** * Send out code for a response from file cache if possible. * * @param ResourceFileCache $fileCache Cache object for this request URL * @param ResourceLoaderContext $context Context in which to generate a response * @param string $etag ETag header value * @return bool If this found a cache file and handled the response */ protected function tryRespondFromFileCache(ResourceFileCache $fileCache, ResourceLoaderContext $context, $etag) { $rlMaxage = $this->config->get('ResourceLoaderMaxage'); // Buffer output to catch warnings. ob_start(); // Get the maximum age the cache can be $maxage = is_null($context->getVersion()) ? $rlMaxage['unversioned']['server'] : $rlMaxage['versioned']['server']; // Minimum timestamp the cache file must have $good = $fileCache->isCacheGood(wfTimestamp(TS_MW, time() - $maxage)); if (!$good) { try { // RL always hits the DB on file cache miss... wfGetDB(DB_SLAVE); } catch (DBConnectionError $e) { // ...check if we need to fallback to cache $good = $fileCache->isCacheGood(); // cache existence check } } if ($good) { $ts = $fileCache->cacheTimestamp(); // Send content type and cache headers $this->sendResponseHeaders($context, $etag, false); $response = $fileCache->fetchText(); // Capture any PHP warnings from the output buffer and append them to the // response in a comment if we're in debug mode. if ($context->getDebug() && strlen($warnings = ob_get_contents())) { $response = self::makeComment($warnings) . $response; } // Remove the output buffer and output the response ob_end_clean(); echo $response . "\n/* Cached {$ts} */"; return true; // cache hit } // Clear buffer ob_end_clean(); return false; // cache miss }
/** * Send out code for a response from file cache if possible * * @param $fileCache ResourceFileCache: Cache object for this request URL * @param $context ResourceLoaderContext: Context in which to generate a response * @return bool If this found a cache file and handled the response */ protected function tryRespondFromFileCache(ResourceFileCache $fileCache, ResourceLoaderContext $context) { global $wgResourceLoaderMaxage; // Buffer output to catch warnings. ob_start(); // Get the maximum age the cache can be $maxage = is_null($context->getVersion()) ? $wgResourceLoaderMaxage['unversioned']['server'] : $wgResourceLoaderMaxage['versioned']['server']; // Minimum timestamp the cache file must have $good = $fileCache->isCacheGood(wfTimestamp(TS_MW, time() - $maxage)); if (!$good) { try { // RL always hits the DB on file cache miss... wfGetDB(DB_SLAVE); } catch (DBConnectionError $e) { // ...check if we need to fallback to cache $good = $fileCache->isCacheGood(); // cache existence check } } if ($good) { $ts = $fileCache->cacheTimestamp(); // Send content type and cache headers $this->sendResponseHeaders($context, $ts, false); // If there's an If-Modified-Since header, respond with a 304 appropriately if ($this->tryRespondLastModified($context, $ts)) { return false; // output handled (buffers cleared) } $response = $fileCache->fetchText(); // Capture any PHP warnings from the output buffer and append them to the // response in a comment if we're in debug mode. if ($context->getDebug() && strlen($warnings = ob_get_contents())) { $response = "/*\n{$warnings}\n*/\n" . $response; } // Remove the output buffer and output the response ob_end_clean(); echo $response . "\n/* Cached {$ts} */"; return true; // cache hit } // Clear buffer ob_end_clean(); return false; // cache miss }