The composite query+supplier cache key.
The result set plus its __cacheMetadata.
Resolves once the write (and any eviction) completes.
await putSupplierQueryCacheEntry("acetone|Loudwolf", { data: [], __cacheMetadata: meta });
export async function putSupplierQueryCacheEntry(
cacheKey: string,
entry: CachedData<unknown>,
): Promise<void> {
try {
const db = await getDB();
const tx = db.transaction(IDB_STORE.SUPPLIER_QUERY_CACHE, 'readwrite');
const store = tx.objectStore(IDB_STORE.SUPPLIER_QUERY_CACHE);
// Evict oldest if at capacity; overwriting an existing key adds no row.
const isNewKey = (await store.getKey(cacheKey)) === undefined;
if (isNewKey && (await store.count()) >= MAX_SUPPLIER_QUERY_CACHE_ENTRIES) {
const index = store.index('cachedAt');
const cursor = await index.openCursor();
if (cursor) {
logger.debug('Evicting oldest supplier query cache entry', {
key: cursor.value.cacheKey,
age:
Math.round((Date.now() - cursor.value.__cacheMetadata.cachedAt) / (60 * 60 * 1000)) +
' hours',
});
await cursor.delete();
}
}
await store.put({
cacheKey,
data: entry.data,
__cacheMetadata: entry.__cacheMetadata,
});
await tx.done;
} catch (error) {
logger.error(
`Failed to put supplier query cache entry in IndexedDB: ${getErrorMessage(error)}`,
{ error },
);
}
}
Writes a supplier query-cache entry, evicting the least recently cached row first when a new key would exceed
storage.maxSupplierQueryCacheEntries.