aboutsummaryrefslogtreecommitdiffstats
path: root/index.ts
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-17 20:00:14 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-17 20:00:14 +0200
commit6cff1bba7d6be3818547b40cc9965f4b2ba73711 (patch)
tree511affe385535730769db93caec3022b473aa453 /index.ts
parent9005f39e1bf83e52ab919bc6be3a1dafe8625cb7 (diff)
downloadsrht-git-feed-6cff1bba7d6be3818547b40cc9965f4b2ba73711.tar.xz
srht-git-feed-6cff1bba7d6be3818547b40cc9965f4b2ba73711.zip
Cache requests by the hour
Diffstat (limited to 'index.ts')
-rw-r--r--index.ts21
1 files changed, 20 insertions, 1 deletions
diff --git a/index.ts b/index.ts
index 5bf62c7..830692b 100644
--- a/index.ts
+++ b/index.ts
@@ -27,6 +27,15 @@ window.onload = function() {
}
}
+const cache = {
+ added: 0,
+ value: {} as QueryResponse,
+ set(data: QueryResponse) {
+ cache.value = data;
+ cache.added = +new Date();
+ }
+}
+
async function getRepositories(): Promise<Repository[] | undefined> {
const response = await fetch(API_URL, {
method: "POST",
@@ -52,7 +61,17 @@ async function getRepositories(): Promise<Repository[] | undefined> {
})
if (response.ok) {
- const json = await response.json() as QueryResponse;
+ let json
+
+ if (cache.added < (+new Date() - 3.6E6)) { // cached for 1 hour
+ json = await response.json() as QueryResponse;
+ cache.set(json);
+ console.log("from api");
+ } else {
+ json = cache.value;
+ console.log("from cache");
+ }
+
return json?.data.repositories.results?.filter(repo => repo.visibility === "PUBLIC") ?? [];
} else {
throw response;