3 * Cache of file objects, wrapping some RepoGroup functions to avoid redundant
4 * queries. Loosely inspired by the LinkCache / LinkBatch classes for titles.
6 * ISSUE: Merge with RepoGroup?
12 var $cache = array(), $notFound = array();
14 protected static $instance;
17 * Get a FileCache instance. Typically, only one instance of FileCache
18 * is needed in a MediaWiki invocation.
20 static function singleton() {
21 if ( self::$instance ) {
22 return self::$instance;
24 self::$instance = new FileCache( RepoGroup::singleton() );
25 return self::$instance;
29 * Destroy the singleton instance, so that a new one will be created next
30 * time singleton() is called.
32 static function destroySingleton() {
33 self::$instance = null;
37 * Set the singleton instance to a given object
39 static function setSingleton( $instance ) {
40 self::$instance = $instance;
44 * Construct a group of file repositories.
45 * @param RepoGroup $repoGroup
47 function __construct( $repoGroup ) {
48 $this->repoGroup = $repoGroup;
53 * Add some files to the cache. This is a fairly low-level function,
54 * which most users should not need to call. Note that any existing
55 * entries for the same keys will not be replaced. Call clearFiles()
56 * first if you need that.
57 * @param array $files array of File objects, indexed by DB key
59 function addFiles( $files ) {
60 wfDebug( "FileCache adding ".count( $files )." files\n" );
61 $this->cache += $files;
65 * Remove some files from the cache, so that their existence will be
66 * rechecked. This is a fairly low-level function, which most users
67 * should not need to call.
68 * @param array $remove array indexed by DB keys to remove (the values are ignored)
70 function clearFiles( $remove ) {
71 wfDebug( "FileCache clearing data for ".count( $remove )." files\n" );
72 $this->cache = array_diff_keys( $this->cache, $remove );
73 $this->notFound = array_diff_keys( $this->notFound, $remove );
77 * Mark some DB keys as nonexistent. This is a fairly low-level
78 * function, which most users should not need to call.
79 * @param array $dbkeys array of DB keys
81 function markNotFound( $dbkeys ) {
82 wfDebug( "FileCache marking ".count( $dbkeys )." files as not found\n" );
83 $this->notFound += array_fill_keys( $dbkeys, true );
88 * Search the cache for a file.
89 * @param mixed $title Title object or string
90 * @return File object or false if it is not found
91 * @todo Implement searching for old file versions(?)
93 function findFile( $title ) {
94 if( !( $title instanceof Title ) ) {
95 $title = Title::makeTitleSafe( NS_FILE, $title );
98 return false; // invalid title?
101 $dbkey = $title->getDBkey();
102 if( array_key_exists( $dbkey, $this->cache ) ) {
103 wfDebug( "FileCache HIT for $dbkey\n" );
104 return $this->cache[$dbkey];
106 if( array_key_exists( $dbkey, $this->notFound ) ) {
107 wfDebug( "FileCache negative HIT for $dbkey\n" );
111 // Not in cache, fall back to a direct query
112 $file = $this->repoGroup->findFile( $title );
114 wfDebug( "FileCache MISS for $dbkey\n" );
115 $this->cache[$dbkey] = $file;
117 wfDebug( "FileCache negative MISS for $dbkey\n" );
118 $this->notFound[$dbkey] = true;
124 * Search the cache for multiple files.
125 * @param array $titles Title objects or strings to search for
126 * @return array of File objects, indexed by DB key
128 function findFiles( $titles ) {
129 $titleObjs = array();
130 foreach ( $titles as $title ) {
131 if ( !( $title instanceof Title ) ) {
132 $title = Title::makeTitleSafe( NS_FILE, $title );
135 $titleObjs[$title->getDBkey()] = $title;
139 $result = array_intersect_key( $this->cache, $titleObjs );
141 $unsure = array_diff_key( $titleObjs, $result, $this->notFound );
143 wfDebug( "FileCache MISS for ".count( $unsure )." files out of ".count( $titleObjs )."...\n" );
144 // XXX: We assume the array returned by findFiles() is
145 // indexed by DBkey; this appears to be true, but should
146 // be explicitly documented.
147 $found = $this->repoGroup->findFiles( $unsure );
149 $this->addFiles( $found );
150 $this->markNotFound( array_keys( array_diff_key( $unsure, $found ) ) );
153 wfDebug( "FileCache found ".count( $result )." files out of ".count( $titleObjs )."\n" );