From 01da0764b2737a3e8672a4a042ee105e8635b2a0 Mon Sep 17 00:00:00 2001 From: HighImKevin Date: Thu, 1 Oct 2026 06:49:06 -0400 Subject: [PATCH] Fixed issue with unnecessary duplicate indexeddb reads --- scripts/indexedDBCache.js | 54 ++++++++----------------------- scripts/itemDetailsCollections.js | 5 ++- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/scripts/indexedDBCache.js b/scripts/indexedDBCache.js index de40daa..db9f6fb 100644 --- a/scripts/indexedDBCache.js +++ b/scripts/indexedDBCache.js @@ -53,41 +53,8 @@ return `${this.prefix}${cacheName}_${user}`; } - // Check if cache is valid (not expired) - async isCacheValid(cacheName, userId = null) { - const key = this.getCacheKey(cacheName, userId); - - try { - const db = await this.openDB(); - const transaction = db.transaction([STORE_NAME], 'readonly'); - const store = transaction.objectStore(STORE_NAME); - const request = store.get(key); - - return new Promise((resolve) => { - request.onsuccess = () => { - const data = request.result; - if (!data) { - resolve(false); - return; - } - - const now = Date.now(); - const ttl = data.ttl || this.ttl; - resolve((now - data.timestamp) < ttl); - }; - - request.onerror = () => { - WARN('Error checking cache validity:', request.error); - resolve(false); - }; - }); - } catch (error) { - ERR('Error in isCacheValid:', error); - return false; - } - } - - // Get cached data entry (payload + metadata) if valid + // Get cached data entry (payload + metadata). Does NOT filter by TTL — + // callers (e.g. apiHelper.getQuery) may intentionally use stale rows. async getCacheEntry(cacheName, userId = null) { const key = this.getCacheKey(cacheName, userId); @@ -124,12 +91,8 @@ } } - // Get cached data if valid + // Get cached payload if present and not expired (single store.get) async get(cacheName, userId = null) { - if (!(await this.isCacheValid(cacheName, userId))) { - return null; - } - const key = this.getCacheKey(cacheName, userId); try { @@ -141,7 +104,16 @@ return new Promise((resolve) => { request.onsuccess = () => { const data = request.result; - resolve(data ? data.payload : null); + if (!data) { + resolve(null); + return; + } + const ttl = data.ttl || this.ttl; + if ((Date.now() - data.timestamp) >= ttl) { + resolve(null); + return; + } + resolve(data.payload); }; request.onerror = () => { diff --git a/scripts/itemDetailsCollections.js b/scripts/itemDetailsCollections.js index 27acc1f..cc32e5d 100644 --- a/scripts/itemDetailsCollections.js +++ b/scripts/itemDetailsCollections.js @@ -116,12 +116,11 @@ LOG('Collections Child Count has changed, invalidating cache'); } - if (!invalidate && await cache.isCacheValid(CACHE_NAME)) { + const currentCacheData = await cache.get(CACHE_NAME); + if (!invalidate && currentCacheData != null) { LOG('Collections cache is still valid'); return; } - - const currentCacheData = await cache.get(CACHE_NAME); const hasExistingCache = Array.isArray(currentCacheData) && currentCacheData.length > 0; if (state.populateInFlight) {