From aa165827fdeba6bb4c0602f0a0168570d6379dd9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 15 Oct 2024 14:03:32 -0700 Subject: incremental location cache update --- lib/locations.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/locations.ts b/lib/locations.ts index 0faad2fc..122a6c3a 100644 --- a/lib/locations.ts +++ b/lib/locations.ts @@ -5,12 +5,11 @@ import { fields, pick } from "$lib/utils"; export let getLocations = async () => { try { - let last = await g("locations:last"); - let now = Date.now(); + let since = await g("locations:since"); + if (!since) since = "2022-09-19T00:00:00Z"; - if (now - last < 60000) return g("locations"); let locations: Array = await got( - "https://api.btcmap.org/v2/elements?updated_since=2022-09-19T00:00:00Z", + `https://api.btcmap.org/v2/elements?updated_since=${since}`, ).json(); locations = locations.filter( @@ -37,8 +36,11 @@ export let getLocations = async () => { } } + let previous = await g("locations"); + locations.push(...previous); + await s("locations", locations); - await s("locations:last", now); + await s("locations:since", new Date().toISOString().split(".")[0] + "Z"); } catch (e) { err("problem fetching locations", e); } -- cgit v1.2.3