diff --git a/web/apps/photos/src/components/ml/PeopleList.tsx b/web/apps/photos/src/components/ml/PeopleList.tsx index 97fa504a0..54d2843f2 100644 --- a/web/apps/photos/src/components/ml/PeopleList.tsx +++ b/web/apps/photos/src/components/ml/PeopleList.tsx @@ -1,4 +1,3 @@ -import { cached } from "@/next/blob-cache"; import { ensureLocalUser } from "@/next/local-user"; import log from "@/next/log"; import { Skeleton, styled } from "@mui/material"; @@ -169,24 +168,13 @@ const FaceCropImageView: React.FC = ({ if (!url) { blob = undefined; } else { - const user = await ensureLocalUser(); - blob = await cached("face-crops", url, async () => { - try { - log.debug( - () => - `ImageCacheView: regenerate face crop for ${faceId}`, - ); - return machineLearningService.regenerateFaceCrop( - user.token, - user.id, - faceId, - ); - } catch (e) { - log.error( - "ImageCacheView: regenerate face crop failed", - e, - ); - } + blob = await cachedOrNew("face-crops", url, async () => { + const user = await ensureLocalUser(); + return machineLearningService.regenerateFaceCrop( + user.token, + user.id, + faceId, + ); }); } diff --git a/web/packages/next/blob-cache.ts b/web/packages/next/blob-cache.ts index 6b8dc5e42..bc6387123 100644 --- a/web/packages/next/blob-cache.ts +++ b/web/packages/next/blob-cache.ts @@ -198,19 +198,24 @@ const openOPFSCacheWeb = async (name: BlobCacheNamespace) => { }; }; -export async function cached( +/** + * Return a cached blob for {@link key} in {@link cacheName}. If the blob is not + * found in the cache, recreate/fetch it using {@link get}, cache it, and then + * return it. + */ +export const cachedOrNew = async ( cacheName: BlobCacheNamespace, - id: string, + key: string, get: () => Promise, -): Promise { +): Promise => { const cache = await openCache(cacheName); - const cachedBlob = await cache.get(id); + const cachedBlob = await cache.get(key); if (cachedBlob) return cachedBlob; const blob = await get(); - await cache.put2(id, blob); + await cache.put2(key, blob); return blob; -} +}; /** * Delete all cached data.