From 7934bd9d72ab849fa7706840cfc43039dfc5018a Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Thu, 17 Sep 2015 15:30:41 -0700 Subject: [PATCH] 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 --- .../resourceloader/ResourceLoaderModule.php | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) 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; } } -- 2.20.1