From: Ori Livneh Date: Thu, 17 Sep 2015 22:30:41 +0000 (-0700) Subject: ResourceLoaderModule: cache file content hash X-Git-Tag: 1.31.0-rc.0~9942^2 X-Git-Url: http://git.cyclocoop.org/%22.%24h.%22?a=commitdiff_plain;h=7934bd9d72ab849fa7706840cfc43039dfc5018a;p=lhc%2Fweb%2Fwiklou.git ResourceLoaderModule: cache file content hash 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 --- diff --git a/includes/resourceloader/ResourceLoaderModule.php b/includes/resourceloader/ResourceLoaderModule.php index 1a4d1f1309..1d3ffb553c 100644 --- a/includes/resourceloader/ResourceLoaderModule.php +++ b/includes/resourceloader/ResourceLoaderModule.php @@ -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; } }