From e8b779745d32bba51b90192fcfa1ce1e192e739d Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 13 Apr 2024 08:33:13 +0530 Subject: [PATCH] put2 --- .../photos/src/services/download/index.ts | 14 ------- web/packages/next/blob-cache.ts | 39 +++++++++---------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/web/apps/photos/src/services/download/index.ts b/web/apps/photos/src/services/download/index.ts index aa9ca68b84..98540ddb6e 100644 --- a/web/apps/photos/src/services/download/index.ts +++ b/web/apps/photos/src/services/download/index.ts @@ -128,20 +128,6 @@ class DownloadManagerImpl { this.progressUpdater = progressUpdater; } - private async getCachedThumbnail(fileID: number) { - try { - const cacheResp: Response = await this.thumbnailCache?.match( - fileID.toString(), - ); - - if (cacheResp) { - return new Uint8Array(await cacheResp.arrayBuffer()); - } - } catch (e) { - log.error("failed to get cached thumbnail", e); - throw e; - } - } private async getCachedFile(file: EnteFile): Promise { const fileCache = this.fileCache; if (!fileCache) return null; diff --git a/web/packages/next/blob-cache.ts b/web/packages/next/blob-cache.ts index 25f1c76bfa..6b8dc5e421 100644 --- a/web/packages/next/blob-cache.ts +++ b/web/packages/next/blob-cache.ts @@ -60,11 +60,12 @@ export interface BlobCache { get: (key: string) => Promise; match: (key: string) => Promise; /** - * Add the given {@link key}-value ({@link data}) pair to the cache. + * Add the given {@link key}-value ({@link blob}) pair to the cache. */ + put2: (key: string, blob: Blob) => Promise; put: (key: string, data: Response) => Promise; /** - * Delete the data corresponding to the given {@link key}. + * Delete the blob corresponding to the given {@link key}. * * The returned promise resolves to `true` if a cache entry was found, * otherwise it resolves to `false`. @@ -125,11 +126,13 @@ const openWebCache = async (name: BlobCacheNamespace) => { return { get: async (key: string) => { const res = await cache.match(key); + console.log("found cache hit", key, res); return await res?.blob(); }, match: (key: string) => { return cache.match(key); }, + put2: (key: string, blob: Blob) => cache.put(key, new Response(blob)), put: (key: string, data: Response) => { return cache.put(key, data); }, @@ -170,10 +173,15 @@ const openOPFSCacheWeb = async (name: BlobCacheNamespace) => { match: (key: string) => { return cache.match(key); }, + put2: async (key: string, blob: Blob) => { + const fileHandle = await _cache.getFileHandle(key, { + create: true, + }); + const writable = await fileHandle.createWritable(); + await writable.write(blob); + await writable.close(); + }, put: async (key: string, data: Response) => { - // const fileHandle = await _cache.getFileHandle(key, { create: true }) - // await fileHandle.write(data); - // await fileHandle.close(); await cache.put(key, data); }, delete: (key: string) => { @@ -196,23 +204,12 @@ export async function cached( get: () => Promise, ): Promise { const cache = await openCache(cacheName); - const cacheResponse = await cache.match(id); + const cachedBlob = await cache.get(id); + if (cachedBlob) return cachedBlob; - let result: Blob; - if (cacheResponse) { - result = await cacheResponse.blob(); - } else { - result = await get(); - - try { - await cache.put(id, new Response(result)); - } catch (e) { - // TODO: handle storage full exception. - console.error("Error while storing file to cache: ", id); - } - } - - return result; + const blob = await get(); + await cache.put2(id, blob); + return blob; } /**