Skip to content

Commit

Permalink
Memoize hashes to FS
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroby0 committed Aug 6, 2021
1 parent 9d13b54 commit 65fa2f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sample/img/
sample/.cache/
package-lock.json
.cache
.image-hash-cache.js

# generated by tests
test/generated-*
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test
sample
.cache
.cache
.image-hash-cache.js
26 changes: 22 additions & 4 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,24 @@ function getValidWidths(originalWidth, widths = [], allowUpscale = false) {
return filtered.sort((a, b) => a - b);
}

// Try to read memoized hashes from filesystem
let imgHashCache = {};
try {
imgHashCache = require(__dirname+"/.image-hash-cache.js");
} catch(err) {
if(err.code !== 'MODULE_NOT_FOUND') {console.log(err);}
}
// Save hashes before exiting.
process.on("exit", () => {
fs.writeFileSync(__dirname+"/.image-hash-cache.js", "module.exports = " + JSON.stringify(imgHashCache));
});
function getHash(src, imgOptions={}, length=10) {
if(src in imgHashCache) return imgHashCache[src];

if(src in imgHashCache) {
if(imgHashCache[src]["type"] === "notFile") return imgHashCache[src]["hash"];
if(imgHashCache[src]["mtime"] === fs.statSync(src).mtimeMs) return imgHashCache[src]["hash"];
}
imgHashCache[src] = {};

const hash = createHash("sha256");

let opts = Object.assign({
Expand All @@ -149,14 +163,18 @@ function getHash(src, imgOptions={}, length=10) {
if(fs.existsSync(src)) {
const fileContent = fs.readFileSync(src);
hash.update(fileContent);
imgHashCache[src]["type"] = "file";
imgHashCache[src]["mtime"] = fs.statSync(src).mtimeMs;
} else {
hash.update(src);
imgHashCache[src]["type"] = "notFile";
imgHashCache[src]["mtime"] = 0;
}

hash.update(JSON.stringify(opts));
imgHashCache[src] = hash.digest("base64url").substring(0, length);
imgHashCache[src]["hash"] = hash.digest("base64url").substring(0, length);

return imgHashCache[src];
return imgHashCache[src]["hash"];
}

function getFilename(src, width, format, options = {}) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
],
"ignoredByWatcher": [
"./.cache/*",
"./.image-hash-cache.js",
"./img/*",
"./test/img/*",
"./test/**/generated*"
Expand Down

0 comments on commit 65fa2f0

Please sign in to comment.