diff --git a/package.json b/package.json index 7c2d195..d7d93de 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/AssetCache.js b/src/AssetCache.js index 0ceadc3..f671532 100644 --- a/src/AssetCache.js +++ b/src/AssetCache.js @@ -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"); @@ -113,6 +113,7 @@ class AssetCache { if (typeof this.#customFilename === "string" && this.#customFilename.length > 0) { return this.#customFilename; } + return `eleventy-fetch-${this.hash}`; } @@ -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; } @@ -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) { @@ -251,7 +257,7 @@ class AssetCache { } get cachedObject() { - return this.cache.getKey(this.hash); + return this.cache.get(this.hash); } needsToFetch(duration) { diff --git a/test/AssetCacheTest.js b/test/AssetCacheTest.js index 0e92cac..2b3ef35 100644 --- a/test/AssetCacheTest.js +++ b/test/AssetCacheTest.js @@ -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);