Made enhanced wfMsg() faster
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 21 Oct 2003 13:01:49 +0000 (13:01 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 21 Oct 2003 13:01:49 +0000 (13:01 +0000)
includes/DefaultSettings.php
includes/GlobalFunctions.php
includes/Setup.php
wiki.phtml

index c61486b..0545db6 100644 (file)
@@ -40,7 +40,7 @@ $wgDBmysql4                   = false; # Set to true to use enhanced fulltext search
 # memcached settings
 # See docs/memcached.doc
 #
-$wgUseMemCached     = false;
+$wgUseMemCached     = false; # Will be set to false in Setup.php, if the server isn't working
 $wgMemCachedServers = array( "127.0.0.1:11000" );
 $wgMemCachedDebug   = false;
 
index ae0ae5e..3217588 100644 (file)
@@ -168,69 +168,75 @@ function wfMsgNoDB( $key ) {
 
 function wfMsgReal( $key, $args, $useDB ) {
        global $wgLang, $wgReplacementKeys, $wgMemc, $wgDBname;
-       global $wgUseDatabaseMessages;
-               
-       static $l1cache = array();
+       global $wgUseDatabaseMessages, $wgUseMemCached, $wgOut;
+       global $wgAllMessagesEn;
+
        $fname = "wfMsg";
        wfProfileIn( $fname );
-       $message = false;
-       $l1hit = false;
        
-       # Check for DB suppression
-       if ( !$wgUseDatabaseMessages || !$useDB ) {
+       static $messageCache = false;
+       $memcKey = "$wgDBname:messages";
+       $fname = "wfMsg";
+       $message = false;
+
+       # newFromText is too slow!
+       $title = ucfirst( $key );
+       if ( $messageCache ) {
+               $message = $messageCache[$title];
+       } elseif ( !$wgUseDatabaseMessages || !$useDB ) {
                $message = $wgLang->getMessage( $key );
        }
 
-       # Try L1 cache
-       if ( $message === false && array_key_exists( $key, $l1cache ) ) {
-               $message  = $l1cache[$key];
-               if ( $message === false ) {
-                       $message = $wgLang->getMessage( $key );
+       if ( !$message && $wgUseMemCached ) {
+               # Try memcached
+               if ( !$messageCache ) {
+                       $messageCache = $wgMemc->get( $memcKey );
                }
-               $l1hit = true;
-       }
-
-       # Try memcached
-       if ( $message === false ) {
-               $titleObj = Title::newFromText( $key );
-               $title = $titleObj->getDBkey();
-               $mcKey = "$wgDBname:MediaWiki:title:$title";
-               $message = $wgMemc->get( $mcKey );
+               
+               # If there's nothing in memcached, load all the messages from the database
+               if ( !$messageCache ) {
+                       # Other threads don't need to load the messages if another thread is doing it.
+                       $wgMemc->set( $memcKey, "loading", time() + 60 );
+                       
+                       $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
+                       $res = wfQuery( $sql, DB_READ, $fname );
+                       for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
+                               $messageCache[$row->cur_title] = $row->cur_text;
+                       }
+                       # Save in memcached
+                       $wgMemc->set( $memcKey, $messageCache, time() + 3600 );
+               }
+               if ( is_array( $messageCache ) && array_key_exists( $title, $messageCache ) ) {
+                       $message = $messageCache[$title];
+               } 
        }
 
-       # Try database
-       if ( $message === false) {
+       # If there was no MemCached, load each message from the DB individually
+       if ( !$message ) {
                if ( $useDB ) {
                        $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI . 
                                " AND cur_title='$title'";
                        $res = wfQuery( $sql, DB_READ, $fname );
 
                        if ( wfNumRows( $res ) ) {
-                               # Got it from the database, now store in MemCached
                                $obj = wfFetchObject( $res );
                                $message = $obj->cur_text;
                                wfFreeResult( $res );
-                               $wgMemc->set( $mcKey, $message, 0 /*time() + 1800*/ );
                        }
                }
        }
 
        # Finally, try the array in $wgLang
-       if ( $message === false ) {
+       if ( !$message ) {
                $message = $wgLang->getMessage( $key );
-               $l1cache[$key] = false;
-       } elseif ( !$l1hit && $wgUseDatabaseMessages) {
-               $l1cache[$key] = $message;
-       }
+       } 
        
        # Replace arguments
        if( count( $args ) ) {
                $message = str_replace( $wgReplacementKeys, $args, $message );
        }
-       
        wfProfileOut( $fname );
-
-       if ( "" == $message ) {
+       if ( !$message ) {
                # Failed, message not translated
                return "&lt;$key&gt;";
        }
index e82809f..d12a96b 100644 (file)
@@ -36,8 +36,10 @@ if( $wgUseMemCached ) {
        $wgMemc->set_debug( $wgMemCachedDebug );
 
        # Test it to see if it's working
-       if ( $wgDebugLogFile && !$wgMemc->set( "test", "", 0 ) ) {
+       # This is necessary because otherwise wfMsg would be extremely inefficient
+       if ( !$wgMemc->set( "test", "", 0 ) ) {
                wfDebug( "Memcached error: " . $wgMemc->error_string() . "\n" );
+               $wgUseMemCached = false;
        }
 }
 
index cd2a880..ef7798a 100644 (file)
@@ -24,6 +24,7 @@ $headers = getallheaders();
 foreach ($headers as $name => $value) {
        wfDebug( "$name: $value\n" );
 }
+wfDebug( "\n" );
 
 # Query string fields
 #