From 0e61557cdb9efba1ad089dbf38032d4b41bd958a Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sun, 29 Mar 2009 10:48:07 +0000 Subject: [PATCH] Support caching of old versions --- includes/filerepo/FileCache.php | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/includes/filerepo/FileCache.php b/includes/filerepo/FileCache.php index 7840d1a3b4..328cd11f46 100644 --- a/includes/filerepo/FileCache.php +++ b/includes/filerepo/FileCache.php @@ -9,7 +9,7 @@ */ class FileCache { var $repoGroup; - var $cache = array(), $notFound = array(); + var $cache = array(), $oldCache = array(), $notFound = array(); protected static $instance; @@ -87,10 +87,10 @@ class FileCache { /** * Search the cache for a file. * @param mixed $title Title object or string + * @param string or false $time, old version time * @return File object or false if it is not found - * @todo Implement searching for old file versions(?) */ - function findFile( $title ) { + function findFile( $title, $time = false ) { if( !( $title instanceof Title ) ) { $title = Title::makeTitleSafe( NS_FILE, $title ); } @@ -99,20 +99,37 @@ class FileCache { } $dbkey = $title->getDBkey(); + # Is there a current version cached? if( array_key_exists( $dbkey, $this->cache ) ) { - wfDebug( "FileCache HIT for $dbkey\n" ); - return $this->cache[$dbkey]; + if( !$time || $this->cache[$dbkey]->getTimestamp() === $time ) { + wfDebug( "FileCache HIT for $dbkey\n" ); + return $this->cache[$dbkey]; + } } + # Is there no current version? Then assume no old versions too. if( array_key_exists( $dbkey, $this->notFound ) ) { wfDebug( "FileCache negative HIT for $dbkey\n" ); return false; } + # Is this old version cached? + if( $time && array_key_exists( $dbkey, $this->oldCache ) && + array_key_exists( $time, $this->oldCache[$dbkey] ) ) + { + wfDebug( "FileCache HIT for $dbkey on $time\n" ); + return $this->oldCache[$dbkey][$time]; + } // Not in cache, fall back to a direct query - $file = $this->repoGroup->findFile( $title ); + $file = $this->repoGroup->findFile( $title, $time ); if( $file ) { wfDebug( "FileCache MISS for $dbkey\n" ); - $this->cache[$dbkey] = $file; + if( !$file->isOld() ) { + $this->cache[$dbkey] = $file; // cache the current version + } + if( !array_key_exists( $dbkey, $this->oldCache ) ) { + $this->oldCache[$dbkey] = array(); + } + $this->oldCache[$dbkey][$file->getTimestamp()] = $file; // cache this version } else { wfDebug( "FileCache negative MISS for $dbkey\n" ); $this->notFound[$dbkey] = true; -- 2.20.1