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)
1  2 
includes/filerepo/RepoGroup.php

@@@ -30,19 -30,15 +30,19 @@@ class RepoGroup 
        /** @var LocalRepo */
        protected $localRepo;
  
 +      /** @var FileRepo[] */
        protected $foreignRepos;
  
        /** @var bool */
        protected $reposInitialised = false;
  
 +      /** @var array */
        protected $localInfo;
 +
 +      /** @var array */
        protected $foreignInfo;
  
-       /** @var array  */
+       /** @var ProcessCacheLRU  */
        protected $cache;
  
        /** @var RepoGroup */
@@@ -80,7 -76,7 +80,7 @@@
         * It's not enough to just create a superclass ... you have
         * to get people to call into it even though all they know is RepoGroup::singleton()
         *
 -       * @param $instance RepoGroup
 +       * @param RepoGroup $instance
         */
        static function setSingleton( $instance ) {
                self::$instance = $instance;
         *
         * @param array $localInfo Associative array for local repo's info
         * @param array $foreignInfo of repository info arrays.
 -       *     Each info array is an associative array with the 'class' member
 -       *     giving the class name. The entire array is passed to the repository
 -       *     constructor as the first parameter.
 +       *   Each info array is an associative array with the 'class' member
 +       *   giving the class name. The entire array is passed to the repository
 +       *   constructor as the first parameter.
         */
        function __construct( $localInfo, $foreignInfo ) {
                $this->localInfo = $localInfo;
                $this->foreignInfo = $foreignInfo;
-               $this->cache = array();
+               $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
        }
  
        /**
         *
         * @param $title Title|string Title object or string
         * @param array $options Associative array of options:
 -       *     time:           requested time for an archived image, or false for the
 -       *                     current version. An image object will be returned which was
 -       *                     created at the specified time.
 -       *
 -       *     ignoreRedirect: If true, do not follow file redirects
 -       *
 -       *     private:        If true, return restricted (deleted) files if the current
 -       *                     user is allowed to view them. Otherwise, such files will not
 -       *                     be found.
 -       *
 -       *     bypassCache:    If true, do not use the process-local cache of File objects
 -       * @return File object or false if it is not found
 +       *   time:           requested time for an archived image, or false for the
 +       *                   current version. An image object will be returned which was
 +       *                   created at the specified time.
 +       *   ignoreRedirect: If true, do not follow file redirects
 +       *   private:        If true, return restricted (deleted) files if the current
 +       *                   user is allowed to view them. Otherwise, such files will not
 +       *                   be found.
 +       *   bypassCache:    If true, do not use the process-local cache of File objects
 +       * @return File|bool False if title is not found
         */
        function findFile( $title, $options = array() ) {
                if ( !is_array( $options ) ) {
                ) {
                        $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 {
                $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;
        }
  
        /**
 -       * @param $inputItems array
 +       * Search repositories for many files at once.
 +       *
 +       * @param array $items An array of titles, or an array of findFile() options with
 +       *    the "title" option giving the title. Example:
 +       *
 +       *     $findItem = array( 'title' => $title, 'private' => true );
 +       *     $findBatch = array( $findItem );
 +       *     $repo->findFiles( $findBatch );
 +       *
 +       *    No title should appear in $items twice, as the result use titles as keys
 +       * @param int $flags Supports:
 +       *     - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
 +       *       The search title uses the input titles; the other is the final post-redirect title.
 +       *       All titles are returned as string DB keys and the inner array is associative.
 +       * @return array Map of (file name => File objects) for matches
 +       *
 +       * @param array $inputItems
 +       * @param integer $flags
         * @return array
         */
 -      function findFiles( $inputItems ) {
 +      function findFiles( array $inputItems, $flags = 0 ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
                        }
                }
  
 -              $images = $this->localRepo->findFiles( $items );
 +              $images = $this->localRepo->findFiles( $items, $flags );
  
                foreach ( $this->foreignRepos as $repo ) {
                        // Remove found files from $items
                                unset( $items[$name] );
                        }
  
 -                      $images = array_merge( $images, $repo->findFiles( $items ) );
 +                      $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
                }
  
                return $images;
                if ( $redir ) {
                        return $redir;
                }
 +
                foreach ( $this->foreignRepos as $repo ) {
                        $redir = $repo->checkRedirect( $title );
                        if ( $redir ) {
         * Find all instances of files with this keys
         *
         * @param array $hashes base 36 SHA-1 hashes
 -       * @return Array of array of File objects
 +       * @return array of array of File objects
         */
        function findBySha1s( array $hashes ) {
                if ( !$this->reposInitialised ) {
  
        /**
         * Get the repo instance with a given key.
 -       * @param $index string|int
 +       * @param string|int $index
         * @return bool|LocalRepo
         */
        function getRepo( $index ) {
  
        /**
         * Get the repo instance by its name
 -       * @param $name string
 +       * @param string $name
         * @return bool
         */
        function getRepoByName( $name ) {
         * Call a function for each foreign repo, with the repo object as the
         * first parameter.
         *
 -       * @param $callback Callback: the function to call
 -       * @param array $params optional additional parameters to pass to the function
 +       * @param callable $callback The function to call
 +       * @param array $params Optional additional parameters to pass to the function
         * @return bool
         */
        function forEachForeignRepo( $callback, $params = array() ) {
  
        /**
         * Does the installation have any foreign repos set up?
 -       * @return Boolean
 +       * @return bool
         */
        function hasForeignRepos() {
                return (bool)$this->foreignRepos;
  
        /**
         * Split a virtual URL into repo, zone and rel parts
 -       * @param $url string
 +       * @param string $url
         * @throws MWException
         * @return array containing repo, zone and rel
         */
        }
  
        /**
 -       * @param $fileName string
 +       * @param string $fileName
         * @return array
         */
        function getFileProps( $fileName ) {
                }
        }
  
-       /**
-        * 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 Title|null Title of the file or null to clear all files
 +       * @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() );
                }
        }
  }