Cache preprocessor output in memcached.

This commit is contained in:
Andrew Garrett 2009-02-06 20:27:58 +00:00
parent b992c2d27d
commit cc481275ef
3 changed files with 49 additions and 19 deletions

View file

@ -87,6 +87,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
* New function to convert namespace text for display (only applies on wiki with
LanguageConverter class)
* (bug 17379) Contributions-title is now parsed for magic words.
* Preprocessor output now cached in memcached.
=== Bug fixes in 1.15 ===
* (bug 16968) Special:Upload no longer throws useless warnings.

View file

@ -63,8 +63,40 @@ class Preprocessor_DOM implements Preprocessor {
*/
function preprocessToObj( $text, $flags = 0 ) {
wfProfileIn( __METHOD__ );
wfProfileIn( __METHOD__.'-makexml' );
global $wgMemc;
$cacheKey = wfMemcKey( 'preprocess-xml', md5($text), $flags );
if ( $xml = $wgMemc->get( $cacheKey ) ) {
// From the cache
wfDebugLog( "Preprocessor", "Loaded preprocessor XML from memcached (key $cacheKey)" );
} else {
$xml = $this->preprocessToXml( $text, $flags );
$wgMemc->set( $cacheKey, $xml, 86400 );
wfDebugLog( "Preprocessor", "Saved preprocessor XML to memcached (key $cacheKey)" );
}
wfProfileIn( __METHOD__.'-loadXML' );
$dom = new DOMDocument;
wfSuppressWarnings();
$result = $dom->loadXML( $xml );
wfRestoreWarnings();
if ( !$result ) {
// Try running the XML through UtfNormal to get rid of invalid characters
$xml = UtfNormal::cleanUp( $xml );
$result = $dom->loadXML( $xml );
if ( !$result ) {
throw new MWException( __METHOD__.' generated invalid XML' );
}
}
$obj = new PPNode_DOM( $dom->documentElement );
wfProfileOut( __METHOD__.'-loadXML' );
wfProfileOut( __METHOD__ );
return $obj;
}
function preprocessToXml( $text, $flags = 0 ) {
wfProfileIn( __METHOD__ );
$rules = array(
'{' => array(
'end' => '}',
@ -571,24 +603,9 @@ class Preprocessor_DOM implements Preprocessor {
$stack->rootAccum .= '</root>';
$xml = $stack->rootAccum;
wfProfileOut( __METHOD__.'-makexml' );
wfProfileIn( __METHOD__.'-loadXML' );
$dom = new DOMDocument;
wfSuppressWarnings();
$result = $dom->loadXML( $xml );
wfRestoreWarnings();
if ( !$result ) {
// Try running the XML through UtfNormal to get rid of invalid characters
$xml = UtfNormal::cleanUp( $xml );
$result = $dom->loadXML( $xml );
if ( !$result ) {
throw new MWException( __METHOD__.' generated invalid XML' );
}
}
$obj = new PPNode_DOM( $dom->documentElement );
wfProfileOut( __METHOD__.'-loadXML' );
wfProfileOut( __METHOD__ );
return $obj;
return $xml;
}
}

View file

@ -45,6 +45,15 @@ class Preprocessor_Hash implements Preprocessor {
*/
function preprocessToObj( $text, $flags = 0 ) {
wfProfileIn( __METHOD__ );
global $wgMemc;
$cacheKey = wfMemckey( 'preprocessor-hash', md5( $text ), $flags );
if ( $obj = $wgMemc->get( $cacheKey ) ) {
wfDebugLog( "Preprocessor", "Got preprocessor_hash output from cache" );
wfProfileOut( __METHOD__ );
return $obj;
}
$rules = array(
'{' => array(
@ -618,6 +627,9 @@ class Preprocessor_Hash implements Preprocessor {
$rootNode->firstChild = $stack->rootAccum->firstNode;
$rootNode->lastChild = $stack->rootAccum->lastNode;
wfProfileOut( __METHOD__ );
$wgMemc->set( $cacheKey, $rootNode, 86400 );
return $rootNode;
}
}