Cache preprocessor output in memcached.
authorAndrew Garrett <werdna@users.mediawiki.org>
Fri, 6 Feb 2009 20:27:58 +0000 (20:27 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Fri, 6 Feb 2009 20:27:58 +0000 (20:27 +0000)
RELEASE-NOTES
includes/parser/Preprocessor_DOM.php
includes/parser/Preprocessor_Hash.php

index 843e9cf..d64659d 100644 (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.
index 502d81f..66b5e1d 100644 (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;
        }
 }
 
index 0e0fb57..0bebdb7 100644 (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;
        }
 }