From 3a07d7f82bde7e29c4355b00f91545cc7b8651f5 Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Mon, 10 Jan 2022 16:33:41 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=99=20not=20checking=20for=20cache=20e?= =?UTF-8?q?xpiry=20only=20at=20start=20now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/yt/urlCache.ts | 65 ++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/src/common/yt/urlCache.ts b/src/common/yt/urlCache.ts index 1a47c8d..2e360c1 100644 --- a/src/common/yt/urlCache.ts +++ b/src/common/yt/urlCache.ts @@ -9,20 +9,44 @@ if (typeof self.indexedDB !== 'undefined') { // Delete Expired openRequest.addEventListener('success', () => { db = openRequest.result - const transaction = db.transaction("store", "readwrite") - const range = IDBKeyRange.upperBound(new Date()) - - const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range) - expireAtCursorRequest.addEventListener('success', () => { - const expireCursor = expireAtCursorRequest.result - if (!expireCursor) return - expireCursor.delete() - expireCursor.continue() - }) + clearExpired() }) } else console.warn(`IndexedDB not supported`) +async function clearExpired() { + return new Promise((resolve, reject) => { + if (!db) throw new Error(`IDBDatabase not defined.`) + const transaction = db.transaction("store", "readwrite") + const range = IDBKeyRange.upperBound(new Date()) + + const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range) + expireAtCursorRequest.addEventListener('error', () => reject(expireAtCursorRequest.error)) + expireAtCursorRequest.addEventListener('success', () => { + try { + const expireCursor = expireAtCursorRequest.result + if (!expireCursor) return + expireCursor.delete() + expireCursor.continue() + resolve() + } + catch (ex) { + reject(ex) + } + }) + }) +} + +async function clearAll() +{ + return await new Promise((resolve, reject) => { + const store = db?.transaction("store", "readwrite").objectStore("store") + if (!store) return resolve() + const request = store.clear() + request.addEventListener('success', () => resolve()) + request.addEventListener('error', () => reject(request.error)) + }) +} async function put(url: string | null, id: string): Promise { return await new Promise((resolve, reject) => { @@ -34,15 +58,26 @@ async function put(url: string | null, id: string): Promise { }) } -async function get(id: string): Promise { - return (await new Promise((resolve, reject) => { +// string means there is cache of lbrypathname +// null means there is cache of that id has no lbrypathname +// undefined means there is no cache +async function get(id: string): Promise { + const response = (await new Promise((resolve, reject) => { const store = db?.transaction("store", "readonly").objectStore("store") - if (!store) return resolve(null) + if (!store) return reject(`Can't find object store.`) + const request = store.get(id) request.addEventListener('success', () => resolve(request.result)) request.addEventListener('error', () => reject(request.error)) - }) as any)?.value + }) as { value: string | null, expireAt: Date } | undefined) + + if (response === undefined) return undefined + if (response.expireAt <= new Date()) { + await clearExpired() + return undefined + } + return response.value } -export const LbryPathnameCache = { put, get } +export const LbryPathnameCache = { put, get, clearAll }