Made Title cache use MapCacheLRU
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 4 Dec 2013 08:27:23 +0000 (00:27 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 4 Dec 2013 08:27:23 +0000 (00:27 -0800)
Change-Id: I913238adc2b8471e247df77150cfd772da1ac3f4

includes/Title.php
includes/cache/MapCacheLRU.php

index 2961500..a29999d 100644 (file)
  * @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 &eacute; &#257; or &#x3017; 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.
index 3539d8f..c66461e 100644 (file)
@@ -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 ) {