From e6ac494beae4a1846bf7b6882f9b14acf7a86105 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 11 Aug 2022 14:13:23 +0530 Subject: [PATCH] replace cache usage with cacherProvider wrapper --- src/services/machineLearning/mlWorkManager.ts | 3 ++- src/utils/machineLearning/faceCrop.ts | 5 +++-- src/utils/machineLearning/mldataExport.ts | 5 +++-- src/utils/storage/cache.ts | 6 ++++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/services/machineLearning/mlWorkManager.ts b/src/services/machineLearning/mlWorkManager.ts index f949726db..0f44ef140 100644 --- a/src/services/machineLearning/mlWorkManager.ts +++ b/src/services/machineLearning/mlWorkManager.ts @@ -11,6 +11,7 @@ import { MLWorkerWithProxy } from 'utils/machineLearning/worker'; import { logError } from 'utils/sentry'; import mlIDbStorage from 'utils/storage/mlIDbStorage'; import { MLSyncJobResult, MLSyncJob } from './mlSyncJob'; +import { getCacheProvider } from 'services/cacheService'; const LIVE_SYNC_IDLE_DEBOUNCE_SEC = 30; const LIVE_SYNC_QUEUE_TIMEOUT_SEC = 300; @@ -105,7 +106,7 @@ class MLWorkManager { this.mlSyncJob = undefined; await this.terminateLiveSyncWorker(); await mlIDbStorage.clearMLDB(); - await caches.delete(FACE_CROPS_CACHE_NAME); + await getCacheProvider().delete(FACE_CROPS_CACHE_NAME); } catch (e) { logError(e, 'Failed in ML logout Handler'); } diff --git a/src/utils/machineLearning/faceCrop.ts b/src/utils/machineLearning/faceCrop.ts index 6bb64b0ae..fe3a59463 100644 --- a/src/utils/machineLearning/faceCrop.ts +++ b/src/utils/machineLearning/faceCrop.ts @@ -1,3 +1,4 @@ +import { getCacheProvider } from 'services/cacheService'; import { compose, Matrix, scale, translate } from 'transformation-matrix'; import { BlobOptions, Dimensions } from 'types/image'; import { @@ -43,7 +44,7 @@ export async function storeFaceCropForBlob( ) { const faceCropUrl = `/${faceId}`; const faceCropResponse = new Response(faceCropBlob); - const faceCropCache = await caches.open(FACE_CROPS_CACHE_NAME); + const faceCropCache = await getCacheProvider().open(FACE_CROPS_CACHE_NAME); await faceCropCache.put(faceCropUrl, faceCropResponse); return { imageUrl: faceCropUrl, @@ -104,7 +105,7 @@ export async function removeOldFaceCrops( export async function removeFaceCropUrls(faceCropUrls: Array) { console.log('Removing face crop urls: ', faceCropUrls); - const faceCropCache = await caches.open(FACE_CROPS_CACHE_NAME); + const faceCropCache = await getCacheProvider().open(FACE_CROPS_CACHE_NAME); const urlRemovalPromises = faceCropUrls?.map((url) => faceCropCache.delete(url) ); diff --git a/src/utils/machineLearning/mldataExport.ts b/src/utils/machineLearning/mldataExport.ts index a3143f926..1a574769e 100644 --- a/src/utils/machineLearning/mldataExport.ts +++ b/src/utils/machineLearning/mldataExport.ts @@ -1,6 +1,7 @@ import { FACE_CROPS_CACHE_NAME, MlFileData } from 'types/machineLearning'; import mlIDbStorage from 'utils/storage/mlIDbStorage'; import * as zip from '@zip.js/zip.js'; +import { getCacheProvider } from 'services/cacheService'; class FileSystemWriter extends zip.Writer { writableStream: FileSystemWritableFileStream; @@ -82,7 +83,7 @@ async function exportMlDataToZipWriter(zipWriter: zip.ZipWriter) { new zip.TextReader(JSON.stringify(mlDbData)) ); - const faceCropCache = await caches.open(FACE_CROPS_CACHE_NAME); + const faceCropCache = await getCacheProvider().open(FACE_CROPS_CACHE_NAME); const files = mlDbData['files'] && (Object.values(mlDbData['files']) as MlFileData[]); for (const fileData of files || []) { @@ -126,7 +127,7 @@ async function importMlDataFromZipReader(zipReader: zip.ZipReader) { // console.log(zipEntries); const faceCropPath = `caches/${FACE_CROPS_CACHE_NAME}`; - const faceCropCache = await caches.open(FACE_CROPS_CACHE_NAME); + const faceCropCache = await getCacheProvider().open(FACE_CROPS_CACHE_NAME); let mldataEntry; for (const entry of zipEntries) { if (entry.filename === 'indexeddb/mldata.json') { diff --git a/src/utils/storage/cache.ts b/src/utils/storage/cache.ts index d7df4f815..702bc5c0a 100644 --- a/src/utils/storage/cache.ts +++ b/src/utils/storage/cache.ts @@ -1,9 +1,11 @@ +import { getCacheProvider } from 'services/cacheService'; + export async function cached( cacheName: string, id: string, get: () => Promise ): Promise { - const cache = await caches.open(cacheName); + const cache = await getCacheProvider().open(cacheName); const cacheResponse = await cache.match(id); let result: Blob; @@ -27,7 +29,7 @@ export async function getBlobFromCache( cacheName: string, url: string ): Promise { - const cache = await caches.open(cacheName); + const cache = await getCacheProvider().open(cacheName); const response = await cache.match(url); return response.blob();