ResourceLoaderModule: cache file content hash
authorOri Livneh <ori@wikimedia.org>
Thu, 17 Sep 2015 22:30:41 +0000 (15:30 -0700)
committerOri.livneh <ori@wikimedia.org>
Fri, 18 Sep 2015 22:27:34 +0000 (22:27 +0000)
Cache the content hash of module files in APC, if available. Include the
modification time in the cached value, so that touching the file invalidates
the cache.

Does this take us back to a world in which updates to a file's mtime cause a
module's version to change? No. The content hash cache will be invalidated,
and the content hash will be computed, but the actual value should remain
the same.

Change-Id: I5ceb8537c3cdb120beae19740635f085ee128272

includes/resourceloader/ResourceLoaderModule.php

index 1a4d1f1..1d3ffb5 100644 (file)
@@ -857,14 +857,35 @@ abstract class ResourceLoaderModule {
         * @return string Hash
         */
        protected static function safeFileHash( $filePath ) {
+               static $cache;
+
+               if ( !$cache ) {
+                       $cache = ObjectCache::newAccelerator( CACHE_NONE );
+               }
+
+               MediaWiki\suppressWarnings();
+               $mtime = filemtime( $filePath );
+               MediaWiki\restoreWarnings();
+               if ( !$mtime ) {
+                       return '';
+               }
+
+               $cacheKey = wfGlobalCacheKey( 'resourceloader', __METHOD__, $filePath );
+               $cachedHash = $cache->get( $cacheKey );
+               if ( isset( $cachedHash['mtime'] ) && $cachedHash['mtime'] === $mtime ) {
+                       return $cachedHash['hash'];
+               }
+
                MediaWiki\suppressWarnings();
                $contents = file_get_contents( $filePath );
                MediaWiki\restoreWarnings();
-               if ( $contents !== false ) {
-                       $hash = hash( 'md4', $contents );
-               } else {
-                       $hash = '';
+               if ( !$contents ) {
+                       return '';
                }
+
+               $hash = hash( 'md4', $contents );
+               $cache->set( $cacheKey, array( 'mtime' => $mtime, 'hash' => $hash ), 60 * 60 * 24 );
+
                return $hash;
        }
 }