thisday: cache

old-stable24
Varun Patil 2022-10-18 11:35:38 -07:00
parent 1b37e8224d
commit e4c9f821fc
2 changed files with 18 additions and 2 deletions

View File

@ -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<IPhoto[]>(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) {

View File

@ -214,10 +214,10 @@ export async function openCache() {
}
/** Get data from the cache */
export async function getCachedData(url: string) {
export async function getCachedData<T>(url: string): Promise<T> {
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();
}