From e4c9f821fc9dc2fee96f44b77e1c1b05a66f3b5b Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Tue, 18 Oct 2022 11:35:38 -0700 Subject: [PATCH] thisday: cache --- src/components/top-matter/OnThisDay.vue | 16 ++++++++++++++++ src/services/Utils.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/components/top-matter/OnThisDay.vue b/src/components/top-matter/OnThisDay.vue index 58b177bc..389ee3b4 100644 --- a/src/components/top-matter/OnThisDay.vue +++ b/src/components/top-matter/OnThisDay.vue @@ -93,7 +93,23 @@ export default class OnThisDay extends Mixins(GlobalMixin) { } async refresh() { + // Look for cache + const dayIdToday = utils.dateToDayId(new Date()); + const cacheUrl = `/onthisday/${dayIdToday}`; + const cache = await utils.getCachedData(cacheUrl); + if (cache) this.process(cache); + + // Network request const photos = await dav.getOnThisDayRaw(); + utils.cacheData(cacheUrl, photos); + + // Check if exactly same as cache + if (cache?.length === photos.length && + cache.every((p, i) => p.fileid === photos[i].fileid)) return; + this.process(photos); + } + + async process(photos: IPhoto[]) { let currentYear = 9999; for (const photo of photos) { diff --git a/src/services/Utils.ts b/src/services/Utils.ts index c05fd0fd..6187c072 100644 --- a/src/services/Utils.ts +++ b/src/services/Utils.ts @@ -214,10 +214,10 @@ export async function openCache() { } /** Get data from the cache */ -export async function getCachedData(url: string) { +export async function getCachedData(url: string): Promise { const cache = staticCache || await openCache(); const cachedResponse = await cache.match(url); - if (!cachedResponse || !cachedResponse.ok) return false; + if (!cachedResponse || !cachedResponse.ok) return undefined; return await cachedResponse.json(); }