Merge "Made RepoGroup use ProcessCacheLRU"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 13 Dec 2013 15:51:15 +0000 (15:51 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 13 Dec 2013 15:51:15 +0000 (15:51 +0000)
includes/cache/MapCacheLRU.php
includes/filerepo/RepoGroup.php

index 3539d8f..26d3ec4 100644 (file)
@@ -28,6 +28,7 @@
  *
  * @see ProcessCacheLRU
  * @ingroup Cache
+ * @since 1.23
  */
 class MapCacheLRU {
        /** @var Array */
index c6e93b6..33ab8ae 100644 (file)
@@ -42,7 +42,7 @@ class RepoGroup {
        /** @var array */
        protected $foreignInfo;
 
-       /** @var array  */
+       /** @var ProcessCacheLRU  */
        protected $cache;
 
        /** @var RepoGroup */
@@ -98,7 +98,7 @@ class RepoGroup {
        function __construct( $localInfo, $foreignInfo ) {
                $this->localInfo = $localInfo;
                $this->foreignInfo = $foreignInfo;
-               $this->cache = array();
+               $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
        }
 
        /**
@@ -137,13 +137,8 @@ class RepoGroup {
                ) {
                        $time = isset( $options['time'] ) ? $options['time'] : '';
                        $dbkey = $title->getDBkey();
-                       if ( isset( $this->cache[$dbkey][$time] ) ) {
-                               wfDebug( __METHOD__ . ": got File:$dbkey from process cache\n" );
-                               # Move it to the end of the list so that we can delete the LRU entry later
-                               $this->pingCache( $dbkey );
-
-                               # Return the entry
-                               return $this->cache[$dbkey][$time];
+                       if ( $this->cache->has( $dbkey, $time, 60 ) ) {
+                               return $this->cache->get( $dbkey, $time );
                        }
                        $useCache = true;
                } else {
@@ -166,8 +161,7 @@ class RepoGroup {
                $image = $image ? $image : false; // type sanity
                # Cache file existence or non-existence
                if ( $useCache && ( !$image || $image->isCacheable() ) ) {
-                       $this->trimCache();
-                       $this->cache[$dbkey][$time] = $image;
+                       $this->cache->set( $dbkey, $time, $image );
                }
 
                return $image;
@@ -453,41 +447,15 @@ class RepoGroup {
                }
        }
 
-       /**
-        * Move a cache entry to the top (such as when accessed)
-        */
-       protected function pingCache( $key ) {
-               if ( isset( $this->cache[$key] ) ) {
-                       $tmp = $this->cache[$key];
-                       unset( $this->cache[$key] );
-                       $this->cache[$key] = $tmp;
-               }
-       }
-
-       /**
-        * Limit cache memory
-        */
-       protected function trimCache() {
-               while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
-                       reset( $this->cache );
-                       $key = key( $this->cache );
-                       wfDebug( __METHOD__ . ": evicting $key\n" );
-                       unset( $this->cache[$key] );
-               }
-       }
-
        /**
         * Clear RepoGroup process cache used for finding a file
         * @param Title|null $title Title of the file or null to clear all files
         */
        public function clearCache( Title $title = null ) {
                if ( $title == null ) {
-                       $this->cache = array();
+                       $this->cache->clear();
                } else {
-                       $dbKey = $title->getDBkey();
-                       if ( isset( $this->cache[$dbKey] ) ) {
-                               unset( $this->cache[$dbKey] );
-                       }
+                       $this->cache->clear( $title->getDBkey() );
                }
        }
 }