r48987 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48986‎ | r48987 | r48988 >
Date:10:48, 29 March 2009
Author:aaron
Status:ok
Tags:
Comment:
Support caching of old versions
Modified paths:
  • /trunk/phase3/includes/filerepo/FileCache.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/FileCache.php
@@ -9,7 +9,7 @@
1010 */
1111 class FileCache {
1212 var $repoGroup;
13 - var $cache = array(), $notFound = array();
 13+ var $cache = array(), $oldCache = array(), $notFound = array();
1414
1515 protected static $instance;
1616
@@ -87,10 +87,10 @@
8888 /**
8989 * Search the cache for a file.
9090 * @param mixed $title Title object or string
 91+ * @param string or false $time, old version time
9192 * @return File object or false if it is not found
92 - * @todo Implement searching for old file versions(?)
9393 */
94 - function findFile( $title ) {
 94+ function findFile( $title, $time = false ) {
9595 if( !( $title instanceof Title ) ) {
9696 $title = Title::makeTitleSafe( NS_FILE, $title );
9797 }
@@ -99,20 +99,37 @@
100100 }
101101
102102 $dbkey = $title->getDBkey();
 103+ # Is there a current version cached?
103104 if( array_key_exists( $dbkey, $this->cache ) ) {
104 - wfDebug( "FileCache HIT for $dbkey\n" );
105 - return $this->cache[$dbkey];
 105+ if( !$time || $this->cache[$dbkey]->getTimestamp() === $time ) {
 106+ wfDebug( "FileCache HIT for $dbkey\n" );
 107+ return $this->cache[$dbkey];
 108+ }
106109 }
 110+ # Is there no current version? Then assume no old versions too.
107111 if( array_key_exists( $dbkey, $this->notFound ) ) {
108112 wfDebug( "FileCache negative HIT for $dbkey\n" );
109113 return false;
110114 }
 115+ # Is this old version cached?
 116+ if( $time && array_key_exists( $dbkey, $this->oldCache ) &&
 117+ array_key_exists( $time, $this->oldCache[$dbkey] ) )
 118+ {
 119+ wfDebug( "FileCache HIT for $dbkey on $time\n" );
 120+ return $this->oldCache[$dbkey][$time];
 121+ }
111122
112123 // Not in cache, fall back to a direct query
113 - $file = $this->repoGroup->findFile( $title );
 124+ $file = $this->repoGroup->findFile( $title, $time );
114125 if( $file ) {
115126 wfDebug( "FileCache MISS for $dbkey\n" );
116 - $this->cache[$dbkey] = $file;
 127+ if( !$file->isOld() ) {
 128+ $this->cache[$dbkey] = $file; // cache the current version
 129+ }
 130+ if( !array_key_exists( $dbkey, $this->oldCache ) ) {
 131+ $this->oldCache[$dbkey] = array();
 132+ }
 133+ $this->oldCache[$dbkey][$file->getTimestamp()] = $file; // cache this version
117134 } else {
118135 wfDebug( "FileCache negative MISS for $dbkey\n" );
119136 $this->notFound[$dbkey] = true;

Status & tagging log