From 9b8d26f6e8725cd9d3959dafb02fc6843f331e4d Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 11 Oct 2018 10:46:48 +0200 Subject: [PATCH] refactor cache to be more generic and usable for other things --- lib/Cache.js | 26 +++++++------------------- lib/Compilation.js | 16 +++++++++------- lib/cache/MemoryCachePlugin.js | 34 ++++++++++------------------------ 3 files changed, 26 insertions(+), 50 deletions(-) diff --git a/lib/Cache.js b/lib/Cache.js index df84e5348..070a200c6 100644 --- a/lib/Cache.js +++ b/lib/Cache.js @@ -13,14 +13,10 @@ const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable"); class Cache { constructor() { this.hooks = { - /** @type {AsyncSeriesBailHook} */ - getModule: new AsyncSeriesBailHook(["identifier"]), - /** @type {AsyncParallelHook} */ - storeModule: new AsyncParallelHook(["identifier", "module"]), /** @type {AsyncSeriesBailHook} */ - getAsset: new AsyncSeriesBailHook(["identifier", "hash"]), - /** @type {AsyncParallelHook} */ - storeAsset: new AsyncParallelHook(["identifier", "hash", "source"]), + get: new AsyncSeriesBailHook(["identifier", "etag"]), + /** @type {AsyncParallelHook} */ + store: new AsyncParallelHook(["identifier", "etag", "data"]), /** @type {SyncHook} */ beginIdle: new SyncHook([]), /** @type {AsyncParallelHook} */ @@ -30,20 +26,12 @@ class Cache { }; } - getModule(identifier, callback) { - this.hooks.getModule.callAsync(identifier, callback); + get(identifier, etag, callback) { + this.hooks.get.callAsync(identifier, etag, callback); } - storeModule(identifier, module, callback) { - this.hooks.storeModule.callAsync(identifier, module, callback); - } - - getAsset(identifier, hash, callback) { - this.hooks.getAsset.callAsync(identifier, hash, callback); - } - - storeAsset(identifier, hash, source, callback) { - this.hooks.storeAsset.callAsync(identifier, hash, source, callback); + store(identifier, etag, source, callback) { + this.hooks.store.callAsync(identifier, etag, source, callback); } beginIdle() { diff --git a/lib/Compilation.js b/lib/Compilation.js index 3dd1738e5..fe4bdea3a 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -499,8 +499,8 @@ class Compilation { return callback(null, alreadyAddedModule); } - const cacheName = this.compilerPath + "/" + identifier; - this.cache.getModule(cacheName, (err, cacheModule) => { + const cacheName = `${this.compilerPath}/module/${identifier}`; + this.cache.get(cacheName, null, (err, cacheModule) => { if (err) return callback(err); if (cacheModule) { @@ -598,8 +598,9 @@ class Compilation { if (currentProfile !== undefined) { currentProfile.markStoringStart(); } - this.cache.storeModule( - this.compilerPath + "/" + module.identifier(), + this.cache.store( + `${this.compilerPath}/module/${module.identifier()}`, + null, module, err => { if (currentProfile !== undefined) { @@ -2066,10 +2067,11 @@ class Compilation { // TODO Workaround for https://github.com/suguru03/neo-async/issues/63 const callback = err => process.nextTick(() => _callback(err)); - const cacheName = this.compilerPath + "/" + fileManifest.identifier; + const ident = fileManifest.identifier; + const cacheName = `${this.compilerPath}/asset/${ident}`; const usedHash = fileManifest.hash; - this.cache.getAsset(cacheName, usedHash, (err, sourceFromCache) => { + this.cache.get(cacheName, usedHash, (err, sourceFromCache) => { let filenameTemplate, file; try { filenameTemplate = fileManifest.filenameTemplate; @@ -2130,7 +2132,7 @@ class Compilation { source, chunk }); - this.cache.storeAsset(cacheName, usedHash, source, callback); + this.cache.store(cacheName, usedHash, source, callback); } catch (err) { this.errors.push( new ChunkRenderError(chunk, file || filenameTemplate, err) diff --git a/lib/cache/MemoryCachePlugin.js b/lib/cache/MemoryCachePlugin.js index 81a581547..001826590 100644 --- a/lib/cache/MemoryCachePlugin.js +++ b/lib/cache/MemoryCachePlugin.js @@ -15,34 +15,20 @@ class MemoryCachePlugin { * @returns {void} */ apply(compiler) { - /** @type {Map} */ - const moduleCache = new Map(); - /** @type {Map} */ - const assetCache = new Map(); - compiler.cache.hooks.storeModule.tap( + /** @type {Map} */ + const cache = new Map(); + compiler.cache.hooks.store.tap( "MemoryCachePlugin", - (identifier, module) => { - moduleCache.set(identifier, module); + (identifier, etag, data) => { + cache.set(identifier, { etag, data }); } ); - compiler.cache.hooks.getModule.tap("MemoryCachePlugin", identifier => { - return moduleCache.get(identifier); + compiler.cache.hooks.get.tap("MemoryCachePlugin", (identifier, etag) => { + const cacheEntry = cache.get(identifier); + if (cacheEntry !== undefined && cacheEntry.etag === etag) { + return cacheEntry.data; + } }); - compiler.cache.hooks.storeAsset.tap( - "MemoryCachePlugin", - (identifier, hash, source) => { - assetCache.set(identifier, { hash, source }); - } - ); - compiler.cache.hooks.getAsset.tap( - "MemoryCachePlugin", - (identifier, hash) => { - const cacheEntry = assetCache.get(identifier); - if (cacheEntry !== undefined && cacheEntry.hash === hash) { - return cacheEntry.source; - } - } - ); } } module.exports = MemoryCachePlugin;