Skip to content

Commit

Permalink
Pre-create sub-directories
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jul 22, 2018
1 parent 58665d3 commit 9f9e831
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/FSCache.js
Expand Up @@ -22,14 +22,25 @@ class FSCache {
);
}

async ensureDirExists(dir = this.dir) {
await fs.mkdirp(dir);
async ensureDirExists() {
if (this.dirExists) {
return;
}

await fs.mkdirp(this.dir);

// Create sub-directories for every possible hex value
// This speeds up large caches on many file systems since there are fewer files in a single directory.
for (let i = 0; i < 256; i++) {
await fs.mkdirp(path.join(this.dir, i.toString(16).padStart(2, '0')));
}

this.dirExists = true;
}

getCacheFile(filename) {
let hash = md5(this.optionsHash + filename);
return path.join(this.dir, hash.substring(0, 2), hash + '.json');
return path.join(this.dir, hash.slice(0, 2), hash.slice(2) + '.json');
}

async getLastModified(filename) {
Expand All @@ -56,8 +67,8 @@ class FSCache {

async write(filename, data) {
try {
await this.ensureDirExists();
let cacheFile = this.getCacheFile(filename);
await this.ensureDirExists(path.dirname(cacheFile));
await this.writeDepMtimes(data);
await fs.writeFile(cacheFile, JSON.stringify(data));
this.invalidated.delete(filename);
Expand Down

0 comments on commit 9f9e831

Please sign in to comment.