Skip to content

Commit

Permalink
Fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 22, 2024
1 parent 22079c6 commit 99b56fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"dependencies": {
"debug": "^4.3.7",
"flat-cache": "^5.0.0",
"flat-cache": "^6.1.1",
"p-queue": "6.6.2"
},
"ava": {
Expand Down
18 changes: 12 additions & 6 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const fsp = fs.promises; // Node 10+
const path = require("path");
const flatCache = require("flat-cache");
const { create: FlatCacheCreate } = require("flat-cache");
const { createHash } = require("crypto");

const debug = require("debug")("Eleventy:Fetch");
Expand Down Expand Up @@ -113,6 +113,7 @@ class AssetCache {
if (typeof this.#customFilename === "string" && this.#customFilename.length > 0) {
return this.#customFilename;
}

return `eleventy-fetch-${this.hash}`;
}

Expand Down Expand Up @@ -141,7 +142,12 @@ class AssetCache {

get cache() {
if (!this._cache || this._cacheLocationDirty) {
this._cache = flatCache.load(this.cacheFilename, this.rootDir);
let cache = FlatCacheCreate({
cacheId: this.cacheFilename,
cacheDir: this.rootDir,
});

this._cache = cache;
}
return this._cache;
}
Expand Down Expand Up @@ -204,12 +210,12 @@ class AssetCache {
await fsp.writeFile(contentPath, contents);
debug(`Writing ${contentPath}`);

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

this.cache.save(true);
}

async getCachedContents(type) {
Expand Down Expand Up @@ -251,7 +257,7 @@ class AssetCache {
}

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

needsToFetch(duration) {
Expand Down
1 change: 1 addition & 0 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test("Test a save", async (t) => {
await asset.save({ followers: 10 }, "json");

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

fs.unlinkSync(cachePath);
fs.unlinkSync(jsonCachePath);
Expand Down

0 comments on commit 99b56fd

Please sign in to comment.