From d1f6bd332848e9640589d4bc323a7493bb626b98 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 21 Oct 2003 13:01:49 +0000 Subject: [PATCH] Made enhanced wfMsg() faster --- includes/DefaultSettings.php | 2 +- includes/GlobalFunctions.php | 72 +++++++++++++++++++----------------- includes/Setup.php | 4 +- wiki.phtml | 1 + 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index c61486b631..0545db6137 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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; diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index ae0ae5ea24..32175884eb 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 "<$key>"; } diff --git a/includes/Setup.php b/includes/Setup.php index e82809f262..d12a96b5e9 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -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; } } diff --git a/wiki.phtml b/wiki.phtml index cd2a88066a..ef7798a22b 100644 --- a/wiki.phtml +++ b/wiki.phtml @@ -24,6 +24,7 @@ $headers = getallheaders(); foreach ($headers as $name => $value) { wfDebug( "$name: $value\n" ); } +wfDebug( "\n" ); # Query string fields # -- 2.20.1