diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-04-17 20:00:14 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-04-17 20:00:14 +0200 |
| commit | 6cff1bba7d6be3818547b40cc9965f4b2ba73711 (patch) | |
| tree | 511affe385535730769db93caec3022b473aa453 /index.ts | |
| parent | 9005f39e1bf83e52ab919bc6be3a1dafe8625cb7 (diff) | |
| download | srht-git-feed-6cff1bba7d6be3818547b40cc9965f4b2ba73711.tar.xz srht-git-feed-6cff1bba7d6be3818547b40cc9965f4b2ba73711.zip | |
Cache requests by the hour
Diffstat (limited to 'index.ts')
| -rw-r--r-- | index.ts | 21 |
1 files changed, 20 insertions, 1 deletions
@@ -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; |
