Interface

This commit is contained in:
Manav Rathi 2024-04-13 08:40:08 +05:30
parent 999f7e45c9
commit a86cdb1f1e
No known key found for this signature in database
2 changed files with 18 additions and 25 deletions

View file

@ -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<FaceCropImageViewProps> = ({
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,
);
});
}

View file

@ -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<Blob>,
): Promise<Blob> {
): Promise<Blob> => {
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.