From 121cf60119814bdfc3a30af37510758c0c6c3c7c Mon Sep 17 00:00:00 2001 From: jeblad Date: Mon, 26 Nov 2012 00:01:24 +0100 Subject: [PATCH] (Bug 38439) Attempt on fixing the suicidal LangObjCache This replaces the suicide of LangObjCache and replaces it with a LRU of configurable length. The patch seems to be functional when I dumped some debug info. Patchset 2: Updated commit message, reformatted a few lines. Patchset 3: Removed the "do not merge" Change-Id: Iee2c796a0c4dd491e31425e04121a1bf0554d7c9 --- includes/DefaultSettings.php | 6 ++++++ languages/Language.php | 23 ++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 60c3ab8754..94bfcc5d7b 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -2165,6 +2165,12 @@ $wgUsePrivateIPs = false; /** Site language code, should be one of ./languages/Language(.*).php */ $wgLanguageCode = 'en'; +/** + * Language cache size, or really how many languages can we handle + * simultanously without degrading to crawl speed. + */ +$wgLangObjCacheSize = 10; + /** * Some languages need different word forms, usually for different cases. * Used in Language::convertGrammar(). diff --git a/languages/Language.php b/languages/Language.php index 437d3c47ca..cc763d09e2 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -172,19 +172,24 @@ class Language { ); /** - * Get a cached language object for a given language code + * Get a cached or new language object for a given language code * @param $code String * @return Language */ static function factory( $code ) { - if ( !isset( self::$mLangObjCache[$code] ) ) { - if ( count( self::$mLangObjCache ) > 10 ) { - // Don't keep a billion objects around, that's stupid. - self::$mLangObjCache = array(); - } - self::$mLangObjCache[$code] = self::newFromCode( $code ); - } - return self::$mLangObjCache[$code]; + global $wgLangObjCacheSize; + + // get the language object to process + $langObj = isset( self::$mLangObjCache[$code] ) + ? self::$mLangObjCache[$code] + : self::newFromCode( $code ); + + // merge the language object in to get it up front in the cache + self::$mLangObjCache = array_merge( array( $code => $langObj ), self::$mLangObjCache ); + // get rid of the oldest ones in case we have an overflow + self::$mLangObjCache = array_slice( self::$mLangObjCache, 0, $wgLangObjCacheSize, true ); + + return $langObj; } /** -- 2.20.1