From 5ac27d708b1ac04e42dd6b11c234d8f65a18905d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 4 Dec 2013 00:27:23 -0800 Subject: [PATCH] Made Title cache use MapCacheLRU Change-Id: I913238adc2b8471e247df77150cfd772da1ac3f4 --- includes/Title.php | 31 +++++++++++++++++-------------- includes/cache/MapCacheLRU.php | 11 ++++++++++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 2961500531..a29999d36b 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -31,10 +31,8 @@ * @internal documentation reviewed 15 Mar 2010 */ class Title { - /** @name Static cache variables */ - // @{ - static private $titleCache = array(); - // @} + /** @var MapCacheLRU */ + static private $titleCache = null; /** * Title::newFromText maintains a cache to avoid expensive re-normalization of @@ -130,6 +128,8 @@ class Title { throw new MWException( 'Title::newFromText given an object' ); } + $cache = self::getTitleCache(); + /** * Wiki pages often contain multiple links to the same page. * Title normalization and parsing can become expensive on @@ -138,8 +138,8 @@ class Title { * * In theory these are value objects and won't get changed... */ - if ( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) { - return Title::$titleCache[$text]; + if ( $defaultNamespace == NS_MAIN && $cache->has( $text ) ) { + return $cache->get( $text ); } # Convert things like é ā or 〗 into normalized (bug 14952) text @@ -149,16 +149,9 @@ class Title { $t->mDbkeyform = str_replace( ' ', '_', $filteredText ); $t->mDefaultNamespace = $defaultNamespace; - static $cachedcount = 0; if ( $t->secureAndSplit() ) { if ( $defaultNamespace == NS_MAIN ) { - if ( $cachedcount >= self::CACHE_MAX ) { - # Avoid memory leaks on mass operations... - Title::$titleCache = array(); - $cachedcount = 0; - } - $cachedcount++; - Title::$titleCache[$text] =& $t; + $cache->set( $text, $t ); } return $t; } else { @@ -200,6 +193,16 @@ class Title { } } + /** + * @return MapCacheLRU + */ + private static function getTitleCache() { + if ( self::$titleCache == null ) { + self::$titleCache = new MapCacheLRU( self::CACHE_MAX ); + } + return self::$titleCache; + } + /** * Returns a list of fields that are to be selected for initializing Title objects or LinkCache entries. * Uses $wgContentHandlerUseDB to determine whether to include page_content_model. diff --git a/includes/cache/MapCacheLRU.php b/includes/cache/MapCacheLRU.php index 3539d8fca2..c66461e86f 100644 --- a/includes/cache/MapCacheLRU.php +++ b/includes/cache/MapCacheLRU.php @@ -66,13 +66,22 @@ class MapCacheLRU { $this->cache[$key] = $value; } + /** + * Check if a key exists + * + * @param $key string + * @return bool + */ + public function has( $key ) { + return isset( $this->cache[$key] ); + } + /** * Get the value for a key. * This returns null if the key is not set. * If the item is already set, it will be pushed to the top of the cache. * * @param $key string - * @param $prop string * @return mixed */ public function get( $key ) { -- 2.20.1