Skip to content

Commit

Permalink
split cache into subdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper committed May 8, 2018
1 parent 87b1ea9 commit 942ddfb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/FSCache.js
Expand Up @@ -20,14 +20,14 @@ class FSCache {
);
}

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

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

async writeDepMtimes(data) {
Expand All @@ -42,9 +42,10 @@ 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(this.getCacheFile(filename), JSON.stringify(data));
await fs.writeFile(cacheFile, JSON.stringify(data));
this.invalidated.delete(filename);
} catch (err) {
logger.error('Error writing to cache', err);
Expand Down Expand Up @@ -83,7 +84,7 @@ class FSCache {

let json = await fs.readFile(cacheFile);
let data = JSON.parse(json);
if (!await this.checkDepMtimes(data)) {
if (!(await this.checkDepMtimes(data))) {
return null;
}

Expand Down

0 comments on commit 942ddfb

Please sign in to comment.