Skip to content

Commit

Permalink
Fix for cache misses.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 25, 2024
1 parent 1cc9fde commit 818313f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
62 changes: 40 additions & 22 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class AssetCache {
});

this._cache = cache;
this._cacheLocationDirty = false;
}
return this._cache;
}
Expand Down Expand Up @@ -182,6 +183,10 @@ class AssetCache {
return `${this.cachePath}.${type}`;
}

get isDirEnsured() {
return this._dirEnsured;
}

async ensureDir() {
if (this._dirEnsured) {
return;
Expand All @@ -203,7 +208,9 @@ class AssetCache {
return;
}

await this.ensureDir();
if(!this.isDirEnsured) {
await this.ensureDir();
}

if (type === "json") {
contents = JSON.stringify(contents);
Expand All @@ -213,14 +220,15 @@ class AssetCache {

// the contents must exist before the cache metadata are saved below
await fsp.writeFile(contentPath, contents);

debug(`Writing ${contentPath}`);

this.cache.set(this.hash, {
cachedAt: Date.now(),
type: type,
});

this.cache.save(true);
this.cache.save();
}

async getCachedContents(type) {
Expand Down Expand Up @@ -257,22 +265,17 @@ class AssetCache {
return this.getCachedContents(type);
}

isCacheValid(duration) {
return this.needsToFetch(duration || this.defaultDuration) === false;
}

get cachedObject() {
return this.cache.get(this.hash);
}

needsToFetch(duration) {
isCacheValid(duration = this.defaultDuration) {
if (!this.cachedObject) {
// not cached
return true;
} else if (!duration || duration === "*") {
return false;
}

// in the cache and no duration
if (!duration || duration === "*") {
// no duration specified (plugin default is 1d, but if this is falsy assume infinite)
// "*" is infinite duration
return false;
return true;
}

debug("Cache check for: %o %o (duration: %o)", this.hash, this.source, duration);
Expand All @@ -284,11 +287,20 @@ class AssetCache {

if (expiration > Date.now()) {
debug("Cache okay, expires in %o s (%o)", expirationRelative / 1000, new Date(expiration));
return false;
return true;
}

debug("Cache expired %o s ago (%o)", expirationRelative / 1000, new Date(expiration));
return true;
return false;
}

get cachedObject() {
return this.cache.get(this.hash);
}

// Deprecated
needsToFetch(duration) {
return !this.isCacheValid(duration);
}

async fetch(options) {
Expand All @@ -299,6 +311,7 @@ class AssetCache {
}

this.log(`Saving ${this.uniqueKey} to ${this.cacheFilename}`);

await this.save(this.source, options.type);

return this.source;
Expand All @@ -311,12 +324,17 @@ class AssetCache {

// for testing
async destroy() {
if (fs.existsSync(this.cachePath)) {
await fsp.unlink(this.cachePath);
}
if (fs.existsSync(this.getCachedContentsPath())) {
await fsp.unlink(this.getCachedContentsPath());
}
let paths = [];
paths.push(this.cachePath);
paths.push(this.getCachedContentsPath("json"));
paths.push(this.getCachedContentsPath("text"));
paths.push(this.getCachedContentsPath("buffer"));

await Promise.all(paths.map(path => {
if (fs.existsSync(path)) {
return fsp.unlink(path);
}
}))
}
}
module.exports = AssetCache;
20 changes: 15 additions & 5 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,33 @@ test("Uses formatUrlForDisplay when caching a promise", async (t) => {

t.truthy(fs.existsSync(jsonCachePath));

fs.unlinkSync(cachePath);
fs.unlinkSync(jsonCachePath);
await asset.destroy();

t.falsy(fs.existsSync(cachePath));
t.falsy(fs.existsSync(jsonCachePath));
});

test("Uses filenameFormat", async (t) => {
let asset = new AssetCache("some-thing", undefined, {
filenameFormat() {
return "testing.json";
// don’t include the file extension
return "testing";
},
});

let cachePath = normalizePath(asset.cachePath);
t.truthy(cachePath.endsWith("/.cache/testing.json"))
let jsonCachePath = normalizePath(asset.getCachedContentsPath("json"));

t.truthy(cachePath.endsWith("/.cache/testing"));
t.truthy(jsonCachePath.endsWith("/.cache/testing.json"));

await asset.save({ name: "Sophia Smith" }, "json");

t.truthy(fs.existsSync(cachePath));
t.truthy(fs.existsSync(jsonCachePath));

await asset.destroy();

asset.destroy();
t.falsy(fs.existsSync(cachePath));
t.falsy(fs.existsSync(jsonCachePath));
});

0 comments on commit 818313f

Please sign in to comment.