diff --git a/img.js b/img.js index b005eac..cb3717f 100644 --- a/img.js +++ b/img.js @@ -172,6 +172,7 @@ class Image { // TODO @zachleat add a smarter cache here (not too aggressive! must handle input file changes) if(!this._contents) { + debug("Reading from file system: %o", this.src); this._contents = fs.readFileSync(this.src); } @@ -292,6 +293,11 @@ class Image { } getHash() { + if (this.computedHash) { + debug("Re-using computed hash for %o: %o", this.src, this.computedHash); + return this.computedHash; + } + let hash = createHash("sha256"); if(fs.existsSync(this.src)) { @@ -343,7 +349,11 @@ class Image { // ANOTHER NOTE: some risk here as I found that not all Nodes have this (e.g. Stackblitz’s Node 16 does not) let base64hash = hash.digest('base64').replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); - return base64hash.slice(0, this.options.hashLength); + const resultHash = base64hash.substring(0, this.options.hashLength); + + this.computedHash = resultHash; + + return resultHash; } getStat(outputFormat, width, height) { @@ -525,7 +535,10 @@ class Image { })); } } - debug( "Wrote %o", stat.outputPath ); + + if(stat.outputPath) { + debug( "Wrote %o", stat.outputPath ); + } } } @@ -615,11 +628,12 @@ function queueImage(src, opts) { key = img.getInMemoryCacheKey(); let cached = memCache.get(key); if(cached) { - debug("Found cached, returning %o", cached); return cached; } } + debug("In-memory cache miss for %o, options: %o", src, opts); + let promise = processingQueue.add(async () => { if(typeof src === "string" && opts && opts.statsOnly) { if(Util.isRemoteUrl(src)) { diff --git a/memory-cache.js b/memory-cache.js index c237afe..f88e50e 100644 --- a/memory-cache.js +++ b/memory-cache.js @@ -3,23 +3,21 @@ const debug = require("debug")("EleventyImg"); class MemoryCache { constructor() { this.cache = {}; - debug("New cache."); } add(key, results) { - debug("Before add cache size %o", Object.keys(this.cache).length); - debug("Added %o to cache: %o", key, results); - this.cache[key] = { results }; + + debug("Added %o to cache (size: %o)", key, Object.keys(this.cache).length); } get(key) { if(this.cache[key]) { // may return promise - debug("Cache size %o", Object.keys(this.cache).length); - debug("Found cached for %o %o", key, this.cache[key].results); + // debug("Cache size %o", Object.keys(this.cache).length); + // debug("Found cached for %o", key); return this.cache[key].results; }