From 6828ddbc765e9756b1f20711eb934c27ee2c544f Mon Sep 17 00:00:00 2001 From: MasterPtato Date: Sun, 16 Jul 2023 20:46:36 +0000 Subject: [PATCH] Reimplement cache version checking --- src/data/cache.ts | 12 ++++++------ src/utils/cache.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/data/cache.ts b/src/data/cache.ts index 6fa07692..d9c2826c 100644 --- a/src/data/cache.ts +++ b/src/data/cache.ts @@ -25,11 +25,11 @@ export namespace ThreadHistoryCache { } export async function get(threadId: string): Promise { - return await readCache(['threads2', threadId, 'history']); + return await readCache(['threads', threadId, 'history']); } export function set(threadId: string, payload: Payload) { - writeCache(['threads2', threadId, 'history'], payload); + writeCache(['threads', threadId, 'history'], payload); } } @@ -40,11 +40,11 @@ export namespace ThreadLiveCache { } export async function get(threadId: string): Promise { - return await readCache(['threads2', threadId]); + return await readCache(['threads', threadId]); } export function set(threadId: string, payload: Payload) { - writeCache(['threads2', threadId], payload); + writeCache(['threads', threadId], payload); } } @@ -55,11 +55,11 @@ export namespace RecentThreadsCache { } export async function get(): Promise { - return await readCache(['threads2', 'recent']); + return await readCache(['threads', 'recent']); } export function set(payload: Payload) { - writeCache(['threads2', 'recent'], payload); + writeCache(['threads', 'recent'], payload); } } diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 58adbb7a..4c3dd789 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -9,7 +9,7 @@ interface CacheWrapper { data: T; ts: number; - // version: string; + version: string; } export interface LocalStorageChange { @@ -144,7 +144,11 @@ export async function readCache(keyPath: string[]): Promise { let wrapper = data; // Cache expired or outdated (used to use blobs) - if (wrapper instanceof Blob || Date.now() - (wrapper.ts as number) >= CACHE_LIFETIME) { + if ( + wrapper instanceof Blob || + Date.now() - wrapper.ts >= CACHE_LIFETIME || + wrapper.version != config.GIT_COMMIT + ) { logging.debug('Cache expired', key); deleteIDB(db, key); @@ -165,7 +169,8 @@ export async function writeCache(keyPath: string[], payload: T) { // Add timestamp and version properties let cacheWrapper: CacheWrapper = { data: payload, - ts: Date.now() + ts: Date.now(), + version: config.GIT_COMMIT }; // Gracefully get DB