wiki.techinc.nl/includes/ParserCache.php

118 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
*
* @addtogroup Cache
* @todo document
*/
class ParserCache {
/**
* 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.
*
* @param object $memCached
*/
2007-01-20 13:34:31 +00:00
function __construct( &$memCached ) {
$this->mMemc =& $memCached;
}
2004-05-27 15:24:04 +00:00
function getKey( &$article, &$user ) {
global $action;
$hash = $user->getPageRenderingHash();
if( !$article->mTitle->quickUserCan( 'edit' ) ) {
// 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');
$key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
2004-05-27 15:24:04 +00:00
return $key;
}
function getETag( &$article, &$user ) {
return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
}
2004-05-27 15:24:04 +00:00
function get( &$article, &$user ) {
global $wgCacheEpoch;
$fname = 'ParserCache::get';
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" );
$value = $this->mMemc->get( $key );
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
$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;
if ( !$canCache || $value->expired( $touched ) ) {
if ( !$canCache ) {
2005-06-19 03:05:51 +00:00
wfIncrStats( "pcache_miss_invalid" );
wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
} else {
2005-06-19 03:05:51 +00:00
wfIncrStats( "pcache_miss_expired" );
wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
}
$this->mMemc->delete( $key );
2004-05-27 15:24:04 +00:00
$value = false;
2005-01-04 12:54:07 +00:00
} else {
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
}
} 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;
}
wfProfileOut( $fname );
2004-05-27 15:24:04 +00:00
return $value;
}
2004-05-27 15:24:04 +00:00
function save( $parserOutput, &$article, &$user ){
global $wgParserCacheExpireTime;
2004-05-27 15:24:04 +00:00
$key = $this->getKey( $article, $user );
2007-04-21 21:35:21 +00:00
if( $parserOutput->getCacheTime() != -1 ) {
2007-04-21 21:35:21 +00:00
$now = wfTimestampNow();
$parserOutput->setCacheTime( $now );
2007-04-21 21:35:21 +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
$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
if( $parserOutput->containsOldMagic() ){
$expire = 3600; # 1 hour
} else {
$expire = $wgParserCacheExpireTime;
}
$this->mMemc->set( $key, $parserOutput, $expire );
2007-04-21 21:35:21 +00:00
} else {
wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
}
}
2007-04-21 21:35:21 +00:00
}