2004-02-26 13:37:26 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
*
|
2007-01-20 15:09:52 +00:00
|
|
|
* @addtogroup Cache
|
2007-04-04 05:22:37 +00:00
|
|
|
* @todo document
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-11-22 01:16:44 +00:00
|
|
|
class ParserCache {
|
2006-01-05 04:26:52 +00:00
|
|
|
/**
|
|
|
|
|
* Get an instance of this object
|
|
|
|
|
*/
|
2006-07-10 15:41:30 +00:00
|
|
|
public static function &singleton() {
|
2006-01-05 04:26:52 +00:00
|
|
|
static $instance;
|
|
|
|
|
if ( !isset( $instance ) ) {
|
|
|
|
|
global $parserMemc;
|
|
|
|
|
$instance = new ParserCache( $parserMemc );
|
|
|
|
|
}
|
|
|
|
|
return $instance;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-11-22 01:16:44 +00:00
|
|
|
/**
|
|
|
|
|
* Setup a cache pathway with a given back-end storage mechanism.
|
|
|
|
|
* May be a memcached client or a BagOStuff derivative.
|
|
|
|
|
*
|
|
|
|
|
* @param object $memCached
|
|
|
|
|
*/
|
2007-01-20 13:34:31 +00:00
|
|
|
function __construct( &$memCached ) {
|
2004-11-22 01:16:44 +00:00
|
|
|
$this->mMemc =& $memCached;
|
|
|
|
|
}
|
2005-07-01 00:03:31 +00:00
|
|
|
|
2004-05-27 15:24:04 +00:00
|
|
|
function getKey( &$article, &$user ) {
|
2006-10-04 09:06:18 +00:00
|
|
|
global $action;
|
2004-02-26 13:37:26 +00:00
|
|
|
$hash = $user->getPageRenderingHash();
|
2007-01-13 03:22:20 +00:00
|
|
|
if( !$article->mTitle->quickUserCan( 'edit' ) ) {
|
2005-12-28 22:28:04 +00:00
|
|
|
// section edit links are suppressed even if the user has them on
|
|
|
|
|
$edit = '!edit=0';
|
|
|
|
|
} else {
|
|
|
|
|
$edit = '';
|
|
|
|
|
}
|
2004-05-27 15:24:04 +00:00
|
|
|
$pageid = intval( $article->getID() );
|
2005-07-03 07:27:43 +00:00
|
|
|
$renderkey = (int)($action == 'render');
|
2006-10-04 09:06:18 +00:00
|
|
|
$key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
|
2004-05-27 15:24:04 +00:00
|
|
|
return $key;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2005-07-01 00:03:31 +00:00
|
|
|
|
|
|
|
|
function getETag( &$article, &$user ) {
|
|
|
|
|
return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-27 15:24:04 +00:00
|
|
|
function get( &$article, &$user ) {
|
2004-11-22 01:16:44 +00:00
|
|
|
global $wgCacheEpoch;
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'ParserCache::get';
|
2004-05-28 05:45:13 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2004-05-27 15:24:04 +00:00
|
|
|
$key = $this->getKey( $article, $user );
|
2005-01-04 12:54:07 +00:00
|
|
|
|
2004-05-30 08:18:40 +00:00
|
|
|
wfDebug( "Trying parser cache $key\n" );
|
2004-11-22 01:16:44 +00:00
|
|
|
$value = $this->mMemc->get( $key );
|
2004-10-23 08:23:13 +00:00
|
|
|
if ( is_object( $value ) ) {
|
2004-05-30 08:18:40 +00:00
|
|
|
wfDebug( "Found.\n" );
|
2004-05-27 15:24:04 +00:00
|
|
|
# Delete if article has changed since the cache was made
|
2004-06-04 10:40:44 +00:00
|
|
|
$canCache = $article->checkTouched();
|
2004-05-30 08:18:40 +00:00
|
|
|
$cacheTime = $value->getCacheTime();
|
2004-06-04 12:31:32 +00:00
|
|
|
$touched = $article->mTouched;
|
2004-11-25 22:02:30 +00:00
|
|
|
if ( !$canCache || $value->expired( $touched ) ) {
|
2004-06-04 10:40:44 +00:00
|
|
|
if ( !$canCache ) {
|
2005-06-19 03:05:51 +00:00
|
|
|
wfIncrStats( "pcache_miss_invalid" );
|
2004-06-04 10:40:44 +00:00
|
|
|
wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
|
|
|
|
|
} else {
|
2005-06-19 03:05:51 +00:00
|
|
|
wfIncrStats( "pcache_miss_expired" );
|
2004-06-04 10:40:44 +00:00
|
|
|
wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
|
|
|
|
|
}
|
2004-11-22 01:16:44 +00:00
|
|
|
$this->mMemc->delete( $key );
|
2004-05-27 15:24:04 +00:00
|
|
|
$value = false;
|
2005-01-04 12:54:07 +00:00
|
|
|
} else {
|
2006-03-16 02:57:44 +00:00
|
|
|
if ( isset( $value->mTimestamp ) ) {
|
|
|
|
|
$article->mTimestamp = $value->mTimestamp;
|
|
|
|
|
}
|
2005-06-19 03:05:51 +00:00
|
|
|
wfIncrStats( "pcache_hit" );
|
2004-05-27 15:24:04 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
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" );
|
2004-05-27 15:24:04 +00:00
|
|
|
$value = false;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-05-28 05:45:13 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2004-05-27 15:24:04 +00:00
|
|
|
return $value;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2005-07-01 00:03:31 +00:00
|
|
|
|
2004-05-27 15:24:04 +00:00
|
|
|
function save( $parserOutput, &$article, &$user ){
|
2006-03-07 01:10:39 +00:00
|
|
|
global $wgParserCacheExpireTime;
|
2004-05-27 15:24:04 +00:00
|
|
|
$key = $this->getKey( $article, $user );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
if( $parserOutput->getCacheTime() != -1 ) {
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
$now = wfTimestampNow();
|
|
|
|
|
$parserOutput->setCacheTime( $now );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
// 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
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
$parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
|
|
|
|
|
wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
if( $parserOutput->containsOldMagic() ){
|
|
|
|
|
$expire = 3600; # 1 hour
|
|
|
|
|
} else {
|
|
|
|
|
$expire = $wgParserCacheExpireTime;
|
|
|
|
|
}
|
|
|
|
|
$this->mMemc->set( $key, $parserOutput, $expire );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
2006-05-13 17:40:59 +00:00
|
|
|
wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|