Skip to content

Commit

Permalink
cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnVN committed Dec 2, 2023
1 parent 52cabd9 commit d8db4f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion functions/chino.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export async function onRequestGet() {
return new Response(await fetch(`https://raw.githubusercontent.com/AutumnVN/chino.webp/main/${Math.ceil(Math.random() * max)}.webp`).then(r => r.blob()), {
headers: {
'content-type': 'image/webp',
'cache-control': 'no-cache, no-store, must-revalidate'
'cache-control': 'no-cache'
}
});
}
2 changes: 1 addition & 1 deletion functions/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function onRequestGet({ env, waitUntil }) {
return new Response(generateSvg(String(value).padStart(7, '0')), {
headers: {
'content-type': 'image/svg+xml',
'cache-control': 'no-cache, no-store, must-revalidate'
'cache-control': 'no-cache'
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion functions/lanyard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function onRequestGet({ env, waitUntil }) {
return new Response(newSvg, {
headers: {
'content-type': 'image/svg+xml',
'cache-control': 'no-cache, no-store, must-revalidate'
'cache-control': 'no-cache'
}
});
}
Expand Down
38 changes: 31 additions & 7 deletions functions/stats.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
export async function onRequestGet({ env }) {
export async function onRequestGet({ env, request, waitUntil }) {
let res = await caches.default.match(request.url);

if (res) {
res = await res.json();
waitUntil(getStats(env).then(r => caches.default.put(request.url, new Response(JSON.stringify(r), {
headers: {
'content-type': 'application/json',
'cache-control': 'public, max-age=3600'
}
}))));
} else {
res = await getStats(env);
waitUntil(caches.default.put(request.url, new Response(JSON.stringify(res), {
headers: {
'content-type': 'application/json',
'cache-control': 'public, max-age=3600'
}
})));
}

return new Response(JSON.stringify(res), {
headers: {
'content-type': 'application/json',
'cache-control': 'no-cache'
}
});
}

async function getStats(env) {
const query = `{
a: repository(owner: "AutumnVN", name: "chino.pages.dev") { stargazers { totalCount } forks { totalCount } }
b: repository(owner: "AutumnVN", name: "debloat-premid") { stargazers { totalCount } forks { totalCount } }
Expand Down Expand Up @@ -59,10 +88,5 @@ export async function onRequestGet({ env }) {

res.push(statsArr);

return new Response(JSON.stringify(res), {
headers: {
'content-type': 'application/json',
'cache-control': 'public, max-age=300'
}
});
return res;
}

0 comments on commit d8db4f0

Please sign in to comment.