Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Cache the caches

Manav Rathi1466ece701f1
diff --git a/web/apps/photos/src/components/ml/PeopleList.tsx b/web/apps/photos/src/components/ml/PeopleList.tsx
index 35ef60e1e9..34293254de 100644
--- a/web/apps/photos/src/components/ml/PeopleList.tsx
+++ b/web/apps/photos/src/components/ml/PeopleList.tsx
@@ -1,4 +1,4 @@
-import { openCache } from "@/next/blob-cache";
+import { blobCache } from "@/next/blob-cache";
import log from "@/next/log";
import { Skeleton, styled } from "@mui/material";
import { Legend } from "components/PhotoViewer/styledComponents/Legend";
@@ -175,7 +175,7 @@ const FaceCropImageView: React.FC<FaceCropImageViewProps> = ({
faceId,
);
})*/
- const cache = await openCache("face-crops");
+ const cache = await blobCache("face-crops");
return await cache.get(faceID);
})
.then((data) => {
diff --git a/web/apps/photos/src/services/download/index.ts b/web/apps/photos/src/services/download/index.ts
index 0618cd0e60..d0be660c9f 100644
--- a/web/apps/photos/src/services/download/index.ts
+++ b/web/apps/photos/src/services/download/index.ts
@@ -1,6 +1,6 @@
import { FILE_TYPE } from "@/media/file-type";
import { decodeLivePhoto } from "@/media/live-photo";
-import { openCache, type BlobCache } from "@/next/blob-cache";
+import { blobCache, type BlobCache } from "@/next/blob-cache";
import log from "@/next/log";
import { APPS } from "@ente/shared/apps/constants";
import ComlinkCryptoWorker from "@ente/shared/crypto";
@@ -91,7 +91,7 @@ class DownloadManagerImpl {
}
this.downloadClient = createDownloadClient(app, tokens);
try {
- this.thumbnailCache = await openCache("thumbs");
+ this.thumbnailCache = await blobCache("thumbs");
} catch (e) {
log.error(
"Failed to open thumbnail cache, will continue without it",
@@ -100,7 +100,7 @@ class DownloadManagerImpl {
}
// TODO (MR): Revisit full file caching cf disk space usage
// try {
- // if (isElectron()) this.fileCache = await openCache("files");
+ // if (isElectron()) this.fileCache = await cache("files");
// } catch (e) {
// log.error("Failed to open file cache, will continue without it", e);
// }
diff --git a/web/apps/photos/src/services/face/f-index.ts b/web/apps/photos/src/services/face/f-index.ts
index dfdd1c926e..eb10c728d4 100644
--- a/web/apps/photos/src/services/face/f-index.ts
+++ b/web/apps/photos/src/services/face/f-index.ts
@@ -1,5 +1,5 @@
import { FILE_TYPE } from "@/media/file-type";
-import { openCache } from "@/next/blob-cache";
+import { blobCache } from "@/next/blob-cache";
import log from "@/next/log";
import { workerBridge } from "@/next/worker/worker-bridge";
import { euclidean } from "hdbscan";
@@ -667,7 +667,7 @@ export const saveFaceCrop = async (imageBitmap: ImageBitmap, face: Face) => {
const blob = await imageBitmapToBlob(faceCrop.image);
- const cache = await openCache("face-crops");
+ const cache = await blobCache("face-crops");
await cache.put(face.id, blob);
faceCrop.image.close();
diff --git a/web/packages/accounts/services/logout.ts b/web/packages/accounts/services/logout.ts
index 70d67b22ff..1858ec7cc3 100644
--- a/web/packages/accounts/services/logout.ts
+++ b/web/packages/accounts/services/logout.ts
@@ -1,4 +1,4 @@
-import { clearCaches } from "@/next/blob-cache";
+import { clearBlobCaches } from "@/next/blob-cache";
import log from "@/next/log";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";
import localForage from "@ente/shared/storage/localForage";
@@ -43,7 +43,7 @@ export const accountLogout = async () => {
log.error("Ignoring error during logout (local forage)", e);
}
try {
- await clearCaches();
+ await clearBlobCaches();
} catch (e) {
log.error("Ignoring error during logout (cache)", e);
}
diff --git a/web/packages/next/blob-cache.ts b/web/packages/next/blob-cache.ts
index e6c3734df2..7223d0fdc1 100644
--- a/web/packages/next/blob-cache.ts
+++ b/web/packages/next/blob-cache.ts
@@ -20,8 +20,8 @@ export type BlobCacheNamespace = (typeof blobCacheNames)[number];
*
* This cache is suitable for storing large amounts of data (entire files).
*
- * To obtain a cache for a given namespace, use {@link openCache}. To clear all
- * cached data (e.g. during logout), use {@link clearCaches}.
+ * To obtain a cache for a given namespace, use {@link openBlobCache}. To clear all
+ * cached data (e.g. during logout), use {@link clearBlobCaches}.
*
* [Note: Caching files]
*
@@ -69,14 +69,31 @@ export interface BlobCache {
delete: (key: string) => Promise<boolean>;
}
+const cachedCaches = new Map<BlobCacheNamespace, BlobCache>();
+
/**
* Return the {@link BlobCache} corresponding to the given {@link name}.
*
+ * This is a wrapper over {@link openBlobCache} that caches (pun intended) the
+ * cache and returns the same one each time it is called with the same name.
+ * It'll open the cache lazily the first time it is invoked.
+ */
+export const blobCache = async (
+ name: BlobCacheNamespace,
+): Promise<BlobCache> => {
+ let c = cachedCaches.get(name);
+ if (!c) cachedCaches.set(name, (c = await openBlobCache(name)));
+ return c;
+};
+
+/**
+ * Create a new {@link BlobCache} corresponding to the given {@link name}.
+ *
* @param name One of the arbitrary but predefined namespaces of type
* {@link BlobCacheNamespace} which group related data and allow us to use the
* same key across namespaces.
*/
-export const openCache = async (
+export const openBlobCache = async (
name: BlobCacheNamespace,
): Promise<BlobCache> =>
isElectron() ? openOPFSCacheWeb(name) : openWebCache(name);
@@ -194,7 +211,7 @@ export const cachedOrNew = async (
key: string,
get: () => Promise<Blob>,
): Promise<Blob> => {
- const cache = await openCache(cacheName);
+ const cache = await openBlobCache(cacheName);
const cachedBlob = await cache.get(key);
if (cachedBlob) return cachedBlob;
@@ -204,15 +221,17 @@ export const cachedOrNew = async (
};
/**
- * Delete all cached data.
+ * Delete all cached data, including cached caches.
*
* Meant for use during logout, to reset the state of the user's account.
*/
-export const clearCaches = async () =>
- isElectron() ? clearOPFSCaches() : clearWebCaches();
+export const clearBlobCaches = async () => {
+ cachedCaches.clear();
+ return isElectron() ? clearOPFSCaches() : clearWebCaches();
+};
const clearWebCaches = async () => {
- await Promise.all(blobCacheNames.map((name) => caches.delete(name)));
+ await Promise.allSettled(blobCacheNames.map((name) => caches.delete(name)));
};
const clearOPFSCaches = async () => {