Manav Rathi 1 سال پیش
والد
کامیت
a86cdb1f1e
2فایلهای تغییر یافته به همراه18 افزوده شده و 25 حذف شده
  1. 7 19
      web/apps/photos/src/components/ml/PeopleList.tsx
  2. 11 6
      web/packages/next/blob-cache.ts

+ 7 - 19
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 { ensureLocalUser } from "@/next/local-user";
 import log from "@/next/log";
 import log from "@/next/log";
 import { Skeleton, styled } from "@mui/material";
 import { Skeleton, styled } from "@mui/material";
@@ -169,24 +168,13 @@ const FaceCropImageView: React.FC<FaceCropImageViewProps> = ({
             if (!url) {
             if (!url) {
                 blob = undefined;
                 blob = undefined;
             } else {
             } 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,
+                    );
                 });
                 });
             }
             }
 
 

+ 11 - 6
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,
     cacheName: BlobCacheNamespace,
-    id: string,
+    key: string,
     get: () => Promise<Blob>,
     get: () => Promise<Blob>,
-): Promise<Blob> {
+): Promise<Blob> => {
     const cache = await openCache(cacheName);
     const cache = await openCache(cacheName);
-    const cachedBlob = await cache.get(id);
+    const cachedBlob = await cache.get(key);
     if (cachedBlob) return cachedBlob;
     if (cachedBlob) return cachedBlob;
 
 
     const blob = await get();
     const blob = await get();
-    await cache.put2(id, blob);
+    await cache.put2(key, blob);
     return blob;
     return blob;
-}
+};
 
 
 /**
 /**
  * Delete all cached data.
  * Delete all cached data.