Skip to content

Commit a109b19

Browse files
committed
Add cache hit to the log, per request from Cloudflare
1 parent aa141ad commit a109b19

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

sample.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@ const AssetCache = saveLocal.AssetCache;
1414
// don’t await here to test concurrency
1515
let first = saveLocal("https://www.zachleat.com/img/avatar-2017-big.png", options);
1616
promises.push(first);
17-
18-
let second = saveLocal("https://www.zachleat.com/img/avatar-2017-big.png", options);
19-
promises.push(second);
20-
21-
promises.push(saveLocal("https://www.zachleat.com/web/css/fonts/lato/2.0/LatoLatin-Regular.ttf", options));
22-
23-
let json = saveLocal("https://opencollective.com/11ty/members/all.json", {
24-
duration: options.duration,
25-
type: "json"
26-
});
27-
promises.push(json);
28-
29-
let asset = new AssetCache("twitter-followers-eleven_ty");
30-
if(asset.isCacheValid("4d")) {
31-
console.log( "Found cached value" );
32-
console.log( await asset.getCachedValue() );
33-
} else {
34-
console.log( "Saving value" );
35-
asset.save({ followers: 42 }, "json");
36-
}
37-
17+
18+
// let second = saveLocal("https://www.zachleat.com/img/avatar-2017-big.png", options);
19+
// promises.push(second);
20+
21+
// promises.push(saveLocal("https://www.zachleat.com/web/css/fonts/lato/2.0/LatoLatin-Regular.ttf", options));
22+
23+
// let json = saveLocal("https://opencollective.com/11ty/members/all.json", {
24+
// duration: options.duration,
25+
// type: "json"
26+
// });
27+
// promises.push(json);
28+
29+
// let asset = new AssetCache("twitter-followers-eleven_ty");
30+
// if(asset.isCacheValid("4d")) {
31+
// console.log( "Found cached value" );
32+
// console.log( await asset.getCachedValue() );
33+
// } else {
34+
// console.log( "Saving value" );
35+
// asset.save({ followers: 42 }, "json");
36+
// }
37+
3838
await Promise.all(promises);
39-
40-
console.log( JSON.stringify(await json).substr(0, 100), "… (truncated)" );
41-
39+
40+
// console.log( JSON.stringify(await json).substr(0, 100), "… (truncated)" );
41+
4242
console.log( process.cpuUsage(startCpu) );
4343
console.log( os.freemem() / (1024 * 1024), os.totalmem() / (1024 * 1024) );
4444
// console.log( process.memoryUsage() );
4545
// console.log( process.resourceUsage() );
46-
46+
4747
})();

src/RemoteAssetCache.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class RemoteAssetCache extends AssetCache {
3535

3636
log(message) {
3737
if(this.options.verbose) {
38-
console.log(message);
38+
console.log(`[11ty/eleventy-fetch] ${message}`);
3939
} else {
4040
debug(message);
4141
}
@@ -63,12 +63,13 @@ class RemoteAssetCache extends AssetCache {
6363
// Important: no disk writes when dryRun
6464
// As of Fetch v4, reads are now allowed!
6565
if(super.isCacheValid(duration) ) {
66+
this.log( `Using cached version of: ${this.displayUrl}` );
6667
return super.getCachedValue();
6768
}
6869

6970
try {
7071
let isDryRun = optionsOverride.dryRun || this.options.dryRun;
71-
this.log( `[11ty/eleventy-fetch] ${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` );
72+
this.log( `${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` );
7273

7374
let fetchOptions = optionsOverride.fetchOptions || this.options.fetchOptions || {};
7475
let response = await fetch(this.url, fetchOptions);
@@ -84,8 +85,8 @@ class RemoteAssetCache extends AssetCache {
8485
return body;
8586
} catch(e) {
8687
if(this.cachedObject) {
87-
this.log( `[11ty/eleventy-fetch] Error fetching ${this.displayUrl}. Message: ${e.message}`);
88-
this.log( `[11ty/eleventy-fetch] Failing gracefully with an expired cache entry.` );
88+
this.log( `Error fetching ${this.displayUrl}. Message: ${e.message}`);
89+
this.log( `Failing gracefully with an expired cache entry.` );
8990
return super.getCachedValue();
9091
} else {
9192
return Promise.reject(e);

0 commit comments

Comments
 (0)