<?php
+/**
+ *
+ * @package MediaWiki
+ * @subpackage Cache
+ */
-class ParserCache
-{
- function get( &$article, &$user ){
+/**
+ *
+ * @package MediaWiki
+ */
+class ParserCache {
+ /**
+ * Setup a cache pathway with a given back-end storage mechanism.
+ * May be a memcached client or a BagOStuff derivative.
+ *
+ * @param object $memCached
+ */
+ function ParserCache( &$memCached ) {
+ $this->mMemc =& $memCached;
+ }
+
+ function getKey( &$article, &$user ) {
+ global $wgDBname, $action;
$hash = $user->getPageRenderingHash();
- $pageid = intval( $id );
- $res = wfQuery("SELECT pc_data FROM parsercache WHERE pc_pageid = {$pageid} ".
- " AND pc_prefhash = '{$hash}' AND pc_expire > NOW()", DB_WRITE);
- $row = wfFetchObject ( $res );
- if( $row ){
- $retVal = unserialize( gzuncompress($row->pc_data) );
- wfProfileOut( $fname );
- } else {
- $retVal = false;
- }
- return $retVal;
+ $pageid = intval( $article->getID() );
+ $renderkey = (int)($action == 'render');
+ $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash";
+ return $key;
}
- function save( $parserOutput, &$article, &$user ){
+ function getETag( &$article, &$user ) {
+ return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
+ }
+
+ function get( &$article, &$user ) {
+ global $wgCacheEpoch;
+ $fname = 'ParserCache::get';
+ wfProfileIn( $fname );
+
$hash = $user->getPageRenderingHash();
$pageid = intval( $article->getID() );
- $title = wfStrencode( $article->mTitle->getPrefixedDBKey() );
- $ser = addslashes( gzcompress( serialize( $parserOutput ) ) );
- if( $parserOutput->containsOldMagic() ){
- $expire = "1 HOUR";
- } else {
- $expire = "7 DAY";
- }
+ $key = $this->getKey( $article, $user );
- wfQuery("REPLACE INTO parsercache (pc_prefhash,pc_pageid,pc_title,pc_data, pc_expire) ".
- "VALUES('{$hash}', {$pageid}, '{$title}', '{$ser}', ".
- "DATE_ADD(NOW(), INTERVAL {$expire}))", DB_WRITE);
+ wfDebug( "Trying parser cache $key\n" );
+ $value = $this->mMemc->get( $key );
+ if ( is_object( $value ) ) {
+ wfDebug( "Found.\n" );
+ # Delete if article has changed since the cache was made
+ $canCache = $article->checkTouched();
+ $cacheTime = $value->getCacheTime();
+ $touched = $article->mTouched;
+ if ( !$canCache || $value->expired( $touched ) ) {
+ if ( !$canCache ) {
+ wfIncrStats( "pcache_miss_invalid" );
+ wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
+ } else {
+ wfIncrStats( "pcache_miss_expired" );
+ wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
+ }
+ $this->mMemc->delete( $key );
+ $value = false;
- if( rand() % 50 == 0 ){ // more efficient to just do it sometimes
- $this->purge();
+ } else {
+ wfIncrStats( "pcache_hit" );
+ }
+ } else {
+ wfDebug( "Parser cache miss.\n" );
+ wfIncrStats( "pcache_miss_absent" );
+ $value = false;
}
- }
-
- function purge(){
- wfQuery("DELETE FROM parsercache WHERE pc_expire < NOW() LIMIT 250", DB_WRITE);
- }
- function clearLinksTo( $pid ){
- $pid = intval( $pid );
- wfQuery("DELETE parsercache FROM parsercache,links ".
- "WHERE pc_title=links.l_from AND l_to={$pid}", DB_WRITE);
- wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
+ wfProfileOut( $fname );
+ return $value;
}
- # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
- function clearBrokenLinksTo( $title ){
- $title = wfStrencode( $title );
- wfQuery("DELETE parsercache FROM parsercache,brokenlinks ".
- "WHERE pc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
- }
+ function save( $parserOutput, &$article, &$user ){
+ $key = $this->getKey( $article, $user );
+ $now = wfTimestampNow();
+ $parserOutput->setCacheTime( $now );
+ $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" );
- # $pid is a page id
- function clearPage( $pid, $namespace ){
- $pid = intval( $pid );
- if( $namespace == NS_MEDIAWIKI ){
- $this->clearLinksTo( $pid );
+ if( $parserOutput->containsOldMagic() ){
+ $expire = 3600; # 1 hour
} else {
- wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
+ $expire = 86400; # 1 day
}
+ $this->mMemc->set( $key, $parserOutput, $expire );
}
}