From f355d2cd847751b50936bc9e325c03e92645f5fa Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sat, 17 Sep 2022 17:28:00 +0530 Subject: [PATCH] create cache Storage service --- src/services/cache/cacheStorageService.ts | 19 +++++++++++++++ src/utils/storage/cache.ts | 28 ++++++----------------- 2 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 src/services/cache/cacheStorageService.ts diff --git a/src/services/cache/cacheStorageService.ts b/src/services/cache/cacheStorageService.ts new file mode 100644 index 000000000..a6939eac4 --- /dev/null +++ b/src/services/cache/cacheStorageService.ts @@ -0,0 +1,19 @@ +import { logError } from 'utils/sentry'; +import { getCacheStorage } from './cacheStorageFactory'; + +async function openCache(cacheName: string) { + try { + return await getCacheStorage().open(cacheName); + } catch (e) { + logError(e, 'openCache failed'); // log and ignore + } +} +async function deleteCache(cacheName: string) { + try { + return await getCacheStorage().delete(cacheName); + } catch (e) { + logError(e, 'deleteCache failed'); // log and ignore + } +} + +export const CacheStorageService = { open: openCache, delete: deleteCache }; diff --git a/src/utils/storage/cache.ts b/src/utils/storage/cache.ts index 8a0cf801c..f05db67b5 100644 --- a/src/utils/storage/cache.ts +++ b/src/utils/storage/cache.ts @@ -1,5 +1,5 @@ -import { FACE_CROPS_CACHE, THUMB_CACHE } from 'constants/cache'; -import { getCacheStorage } from 'services/cache/cacheStorageFactory'; +import { FACE_CROPS_CACHE, FILE_CACHE, THUMB_CACHE } from 'constants/cache'; +import { CacheStorageService } from 'services/cache/cacheStorageService'; import { logError } from 'utils/sentry'; export async function cached( @@ -7,7 +7,7 @@ export async function cached( id: string, get: () => Promise ): Promise { - const cache = await openCache(cacheName); + const cache = await CacheStorageService.open(cacheName); const cacheResponse = await cache.match(id); let result: Blob; @@ -31,31 +31,17 @@ export async function getBlobFromCache( cacheName: string, url: string ): Promise { - const cache = await openCache(cacheName); + const cache = await CacheStorageService.open(cacheName); const response = await cache.match(url); return response.blob(); } -export async function openCache(cacheName: string) { - try { - return await getCacheStorage().open(cacheName); - } catch (e) { - logError(e, 'openCache failed'); // log and ignore - } -} -export async function deleteCache(cacheName: string) { - try { - return await getCacheStorage().delete(cacheName); - } catch (e) { - logError(e, 'deleteCache failed'); // log and ignore - } -} - export async function deleteAllCache() { try { - await deleteCache(THUMB_CACHE); - await deleteCache(FACE_CROPS_CACHE); + await CacheStorageService.delete(THUMB_CACHE); + await CacheStorageService.delete(FACE_CROPS_CACHE); + await CacheStorageService.delete(FILE_CACHE); } catch (e) { logError(e, 'deleteAllCache failed'); // log and ignore }