wiki.techinc.nl/includes/parser/ParserCache.php

191 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/**
* @ingroup Cache Parser
* @todo document
*/
class ParserCache {
private $mMemc;
/**
* Get an instance of this object
*/
public static function singleton() {
static $instance;
if ( !isset( $instance ) ) {
global $parserMemc;
$instance = new ParserCache( $parserMemc );
}
return $instance;
}
2006-01-07 13:31:29 +00:00
/**
* Setup a cache pathway with a given back-end storage mechanism.
* May be a memcached client or a BagOStuff derivative.
*
2010-05-15 20:16:26 +00:00
* @param $memCached Object
*/
function __construct( $memCached ) {
if ( !$memCached ) {
global $parserMemc;
$parserMemc = $memCached = wfGetParserCacheStorage();
}
$this->mMemc = $memCached;
}
protected function getParserOutputKey( $article, $hash ) {
global $wgRequest;
// idhash seem to mean 'page id' + 'rendering hash' (r3710)
$pageid = $article->getID();
$renderkey = (int)($wgRequest->getVal('action') == 'render');
$key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
2004-05-27 15:24:04 +00:00
return $key;
}
protected function getOptionsKey( $article ) {
$pageid = $article->getID();
return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
}
/**
* Provides an E-Tag suitable for the whole page, even if $article is
* just the main wikitext. So it uses the complete set of user options.
* Most importantly, that includes the user language, but other options
* would give problems on some setups, too.
*/
function getETag( $article, $popts ) {
return 'W/"' . $this->getParserOutputKey( $article,
$popts->optionsHash( ParserOptions::legacyOptions() ) ) .
"--" . $article->mTouched . '"';
}
/**
* Retrieve the ParserOutput from ParserCache, even if it's outdated.
*/
public function getDirty( $article, $popts ) {
$value = $this->mMemc->get( $article, $popts, true );
return is_object( $value ) ? $value : false;
}
/**
* Used to provide a unique id for the PoolCounter.
* It would be preferable to have this code in get()
* instead of having Article looking in our internals.
*
* Precondition: $article->checkTouched() has been called.
*/
public function getKey( $article, $popts, $useOutdated = true ) {
global $wgCacheEpoch;
if( $popts instanceof User ) {
wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
$popts = ParserOptions::newFromUser( $popts );
}
// Determine the options which affect this article
$optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
if ( $optionsKey != false ) {
if ( !$useOutdated && $optionsKey->expired( $article->mTouched ) ) {
wfIncrStats( "pcache_miss_expired" );
$cacheTime = $optionsKey->getCacheTime();
wfDebug( "Parser options key expired, touched {$article->mTouched}, epoch $wgCacheEpoch, cached $cacheTime\n" );
return false;
}
$usedOptions = $optionsKey->mUsedOptions;
wfDebug( "Parser cache options found.\n" );
} else {
# TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
$usedOptions = ParserOptions::legacyOptions();
}
return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
}
/**
* Retrieve the ParserOutput from ParserCache.
* false if not found or outdated.
*/
public function get( $article, $popts, $useOutdated = false ) {
global $wgCacheEpoch;
wfProfileIn( __METHOD__ );
$canCache = $article->checkTouched();
if ( !$canCache ) {
// It's a redirect now
wfProfileOut( __METHOD__ );
return false;
}
// Having called checkTouched() ensures this will be loaded
$touched = $article->mTouched;
$parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
if ( $parserOutputKey === false ) {
wfProfileOut( __METHOD__ );
return false;
}
$value = $this->mMemc->get( $parserOutputKey );
if ( !$value ) {
2004-10-23 10:22:38 +00:00
wfDebug( "Parser cache miss.\n" );
2005-06-19 03:05:51 +00:00
wfIncrStats( "pcache_miss_absent" );
wfProfileOut( __METHOD__ );
return false;
}
wfDebug( "Found.\n" );
if ( !$useOutdated && $value->expired( $touched ) ) {
wfIncrStats( "pcache_miss_expired" );
$cacheTime = $value->getCacheTime();
wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
2004-05-27 15:24:04 +00:00
$value = false;
} else {
if ( isset( $value->mTimestamp ) ) {
$article->mTimestamp = $value->mTimestamp;
}
wfIncrStats( "pcache_hit" );
}
wfProfileOut( __METHOD__ );
2004-05-27 15:24:04 +00:00
return $value;
}
public function save( $parserOutput, $article, $popts ) {
$expire = $parserOutput->getCacheExpiry();
2007-04-21 21:35:21 +00:00
if( $expire > 0 ) {
$now = wfTimestampNow();
$optionsKey = new CacheTime;
$optionsKey->mUsedOptions = $popts->usedOptions();
$optionsKey->updateCacheExpiry( $expire );
$optionsKey->setCacheTime( $now );
$parserOutput->setCacheTime( $now );
2007-04-21 21:35:21 +00:00
$optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
$parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
// Save the timestamp so that we don't have to load the revision row on view
$parserOutput->mTimestamp = $article->getTimestamp();
2007-04-21 21:35:21 +00:00
$parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
2007-04-21 21:35:21 +00:00
// Save the parser output
$this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
2007-04-21 21:35:21 +00:00
// ...and its pointer
$this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
} else {
wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
}
}
}