Skip to content

Commit 8c362da

Browse files
committed
Fixes #56 removes flat-cache
1 parent 093f72f commit 8c362da

File tree

5 files changed

+120
-52
lines changed

5 files changed

+120
-52
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
"prettier": "^3.3.3"
4646
},
4747
"dependencies": {
48+
"@11ty/eleventy-utils": "^2.0.1",
4849
"@rgrove/parse-xml": "^4.2.0",
49-
"debug": "^4.3.7",
50-
"flat-cache": "^6.1.1",
50+
"debug": "^4.4.0",
5151
"graceful-fs": "^4.2.11",
5252
"p-queue": "6.6.2"
5353
},

src/AssetCache.js

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const fs = require("graceful-fs");
22
const path = require("path");
3-
const { create: FlatCacheCreate } = require("flat-cache");
43
const { createHash } = require("crypto");
54
const debugUtil = require("debug");
5+
const { DateCompare } = require("@11ty/eleventy-utils");
66

7+
const FileCache = require("./FileCache.js");
78
const Sources = require("./Sources.js");
89
const DirectoryManager = require("./DirectoryManager.js");
910

@@ -18,7 +19,7 @@ class AssetCache {
1819
#cacheDirectory;
1920
#cacheLocationDirty = false;
2021
#directoryManager;
21-
#rawContents = {}
22+
#rawContents = {};
2223

2324
constructor(source, cacheDirectory, options = {}) {
2425
if(!Sources.isValidSource(source)) {
@@ -173,10 +174,11 @@ class AssetCache {
173174

174175
get cache() {
175176
if (!this.#cache || this.#cacheLocationDirty) {
176-
let cache = FlatCacheCreate({
177-
cacheId: this.cacheFilename,
178-
cacheDir: this.rootDir,
177+
let cache = new FileCache(this.cacheFilename, {
178+
dir: this.rootDir
179179
});
180+
cache.setDryRun(this.options.dryRun);
181+
cache.setDirectoryManager(this.#directoryManager);
180182

181183
this.#cache = cache;
182184
this.#cacheLocationDirty = false;
@@ -185,24 +187,7 @@ class AssetCache {
185187
}
186188

187189
getDurationMs(duration = "0s") {
188-
let durationUnits = duration.slice(-1);
189-
let durationMultiplier;
190-
if (durationUnits === "s") {
191-
durationMultiplier = 1;
192-
} else if (durationUnits === "m") {
193-
durationMultiplier = 60;
194-
} else if (durationUnits === "h") {
195-
durationMultiplier = 60 * 60;
196-
} else if (durationUnits === "d") {
197-
durationMultiplier = 60 * 60 * 24;
198-
} else if (durationUnits === "w") {
199-
durationMultiplier = 60 * 60 * 24 * 7;
200-
} else if (durationUnits === "y") {
201-
durationMultiplier = 60 * 60 * 24 * 365;
202-
}
203-
204-
let durationValue = parseInt(duration.slice(0, duration.length - 1), 10);
205-
return durationValue * durationMultiplier * 1000;
190+
return DateCompare.getDurationMs(duration);
206191
}
207192

208193
getCachedContentsPath(type = "buffer") {
@@ -263,8 +248,6 @@ class AssetCache {
263248
// the contents must exist before the cache metadata are saved below
264249
fs.writeFileSync(contentPath, contents);
265250
debug(`Writing ${contentPath}`);
266-
267-
this.cache.save();
268251
}
269252

270253
async #getCachedContents(type) {
@@ -335,27 +318,7 @@ class AssetCache {
335318
return false;
336319
}
337320

338-
// in the cache and no duration
339-
if (!duration || duration === "*") {
340-
// no duration specified (plugin default is 1d, but if this is falsy assume infinite)
341-
// "*" is infinite duration
342-
return true;
343-
}
344-
345-
debug("Cache check for: %o %o (duration: %o)", this.hash, this.source, duration);
346-
debug("Cache object: %o", this.cachedObject);
347-
348-
let compareDuration = this.getDurationMs(duration);
349-
let expiration = this.cachedObject.cachedAt + compareDuration;
350-
let expirationRelative = Math.abs(Date.now() - expiration);
351-
352-
if (expiration > Date.now()) {
353-
debug("Cache okay, expires in %o s (%o)", expirationRelative / 1000, new Date(expiration));
354-
return true;
355-
}
356-
357-
debug("Cache expired %o s ago (%o)", expirationRelative / 1000, new Date(expiration));
358-
return false;
321+
return DateCompare.isTimestampWithinDuration(this.cachedObject.cachedAt, duration);
359322
}
360323

361324
get cachedObject() {

src/DirectoryManager.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const fs = require("node:fs");
2-
const path = require("node:path");
3-
const debugUtil = require("debug");
4-
const debugAssets = debugUtil("Eleventy:Assets");
2+
const debug = require("debug")("Eleventy:Assets");
53

64
class DirectoryManager {
75
#dirs = new Set();
@@ -16,7 +14,7 @@ class DirectoryManager {
1614
}
1715

1816
this.#dirs.add(dir);
19-
debugAssets("[11ty/eleventy-fetch] Creating directory %o", dir);
17+
debug("Creating directory %o", dir);
2018
fs.mkdirSync(dir, { recursive: true });
2119
}
2220
}

src/ExistsCache.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fs = require("node:fs");
2+
// const debug = require("debug")("Eleventy:Assets");
3+
4+
class ExistsCache {
5+
#checks = new Map();
6+
#count = 0;
7+
8+
set(target, value) {
9+
this.#checks.set(target, Boolean(value));
10+
}
11+
12+
exists(target) {
13+
if(this.#checks.has(target)) {
14+
return this.#checks.get(target);
15+
}
16+
17+
let exists = fs.existsSync(target);
18+
this.#count++;
19+
this.#checks.set(target, exists);
20+
return exists;
21+
}
22+
}
23+
24+
module.exports = ExistsCache;

src/FileCache.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const fs = require("node:fs");
2+
const path = require("node:path");
3+
4+
const DirectoryManager = require("./DirectoryManager.js");
5+
const ExistsCache = require("./ExistsCache.js");
6+
7+
let existsCache = new ExistsCache();
8+
9+
class FileCache {
10+
#directoryManager;
11+
#data = {};
12+
#dryRun = false;
13+
#cacheDirectory = ".cache";
14+
#counts = {
15+
read: 0,
16+
write: 0,
17+
}
18+
19+
constructor(cacheId, options = {}) {
20+
this.cacheId = cacheId;
21+
if(options.dir) {
22+
this.#cacheDirectory = options.dir;
23+
}
24+
}
25+
26+
setDryRun(val) {
27+
this.#dryRun = Boolean(val);
28+
}
29+
30+
setDirectoryManager(manager) {
31+
this.#directoryManager = manager;
32+
}
33+
34+
ensureDir() {
35+
if (this.#dryRun || existsCache.exists(this.#cacheDirectory)) {
36+
return;
37+
}
38+
39+
if(!this.#directoryManager) {
40+
// standalone fallback (for tests)
41+
this.#directoryManager = new DirectoryManager();
42+
}
43+
44+
this.#directoryManager.create(this.#cacheDirectory);
45+
}
46+
47+
set(id, obj) {
48+
if(this.#data[id] !== obj) {
49+
this.#data[id] = obj;
50+
this.save();
51+
}
52+
}
53+
54+
get fsPath() {
55+
return path.join(this.#cacheDirectory, this.cacheId);
56+
}
57+
58+
get(id) {
59+
if(this.#data[id]) {
60+
return this.#data[id];
61+
}
62+
63+
if(!existsCache.exists(this.fsPath)) {
64+
return;
65+
}
66+
67+
this.#counts.read++;
68+
69+
let data = fs.readFileSync(this.fsPath, "utf8");
70+
let json = JSON.parse(data);
71+
this.#data[id] = json;
72+
return json;
73+
}
74+
75+
save() {
76+
this.ensureDir(); // doesn’t add to counts (yet?)
77+
78+
this.#counts.write++;
79+
fs.writeFileSync(this.fsPath, JSON.stringify(this.#data), "utf8");
80+
}
81+
}
82+
83+
module.exports = FileCache;

0 commit comments

Comments
 (0)