ChemPal Documentation - v1.6.0
    Preparing search index...

    Function putSupplierProductDataCacheEntry

    • Writes a supplier product-data cache entry, evicting the oldest row first when the store is at maxSupplierCacheEntries capacity.

      Parameters

      • cacheKey: string

        The product-level cache key.

      • entry: CachedProductEntry

        The product data and the timestamp it was fetched.

      Returns Promise<void>

      Resolves once the write (and any eviction) completes.

      await putSupplierProductDataCacheEntry("Loudwolf|SKU-1", { data, timestamp: Date.now() });
      
      export async function putSupplierProductDataCacheEntry(
      cacheKey: string,
      entry: CachedProductEntry,
      ): Promise<void> {
      try {
      const db = await getDB();
      const tx = db.transaction(IDB_STORE.SUPPLIER_PRODUCT_DATA_CACHE, 'readwrite');
      const store = tx.objectStore(IDB_STORE.SUPPLIER_PRODUCT_DATA_CACHE);

      // Evict oldest if at capacity
      const count = await store.count();
      if (count >= MAX_SUPPLIER_CACHE_ENTRIES) {
      const index = store.index('timestamp');
      const cursor = await index.openCursor();
      if (cursor) {
      await cursor.delete();
      }
      }

      await store.put({
      cacheKey,
      data: entry.data,
      timestamp: entry.timestamp,
      });

      await tx.done;
      } catch (error) {
      logger.error('Failed to put supplier product data cache entry in IndexedDB', { error });
      }
      }