From 4bdf147e2b86bbc5c8cee78b0d58744e743a893f Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Mon, 28 Jun 2021 08:25:54 +1000 Subject: [PATCH] Change clear cache function to not load all assets into memory --- src/workers/DatabaseWorker.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/workers/DatabaseWorker.js b/src/workers/DatabaseWorker.js index 8b53785..e58774f 100644 --- a/src/workers/DatabaseWorker.js +++ b/src/workers/DatabaseWorker.js @@ -184,25 +184,24 @@ let service = { try { let db = getDatabase({}); const userId = (await db.table("user").get("userId")).value; - const cachedAssets = await db + + const assetSizes = []; + await db .table("assets") .where("owner") .notEqual(userId) - .toArray(); - const totalSize = cachedAssets.reduce( - (acc, cur) => acc + cur.file.byteLength, - 0 - ); + .each((asset) => { + assetSizes.push({ id: asset.id, size: asset.file.byteLength }); + }); + const totalSize = assetSizes.reduce((acc, cur) => acc + cur.size, 0); if (totalSize > maxCacheSize) { // Remove largest assets first - const largestAssets = cachedAssets.sort( - (a, b) => b.file.byteLength - a.file.byteLength - ); + const largestAssets = assetSizes.sort((a, b) => b.size - a.size); let assetsToDelete = []; let deletedBytes = 0; for (let asset of largestAssets) { assetsToDelete.push(asset.id); - deletedBytes += asset.file.byteLength; + deletedBytes += asset.size; if (totalSize - deletedBytes < maxCacheSize) { break; }