add cache service

This commit is contained in:
Abhinav 2022-08-11 10:41:38 +05:30
parent 8bb63f0780
commit 8c21a1f833
3 changed files with 35 additions and 18 deletions

View file

@ -0,0 +1,31 @@
import electronService from './electron/common';
import electronCacheService from './electron/cache';
import { logError } from 'utils/sentry';
const THUMB_CACHE = 'thumbs';
export function getCacheProvider() {
if (electronService.checkIsBundledApp()) {
return electronCacheService;
} else {
return caches;
}
}
export async function openThumbnailCache() {
try {
return await getCacheProvider().open(THUMB_CACHE);
} catch (e) {
logError(e, 'openThumbnailCache failed');
// log and ignore
}
}
export async function deleteThumbnailCache() {
try {
return await getCacheProvider().delete(THUMB_CACHE);
} catch (e) {
logError(e, 'deleteThumbnailCache failed');
// dont throw
}
}

View file

@ -12,10 +12,7 @@ import { EnteFile } from 'types/file';
import { logError } from 'utils/sentry';
import { FILE_TYPE } from 'constants/file';
import { CustomError } from 'utils/error';
import electronService from 'services/electron/common';
import electronCacheService from 'services/electron/cache';
const THUMB_CACHE = 'thumbs';
import { openThumbnailCache } from './cacheService';
class DownloadManager {
private fileObjectURLPromise = new Map<string, Promise<string[]>>();
@ -29,19 +26,7 @@ class DownloadManager {
}
if (!this.thumbnailObjectURLPromise.get(file.id)) {
const downloadPromise = async () => {
const thumbnailCache = await (async () => {
try {
if (electronService.checkIsBundledApp()) {
return await electronCacheService.open(
THUMB_CACHE
);
} else {
return await caches.open(THUMB_CACHE);
}
} catch (e) {
logError(e, 'cache open failed');
}
})();
const thumbnailCache = await openThumbnailCache();
const cacheResp: Response = await thumbnailCache?.match(
file.id.toString()

View file

@ -21,6 +21,7 @@ import { getLocalFamilyData, isPartOfFamily } from 'utils/billing';
import { ServerErrorCodes } from 'utils/error';
import isElectron from 'is-electron';
import safeStorageService from './electron/safeStorage';
import { deleteThumbnailCache } from './cacheService';
const ENDPOINT = getEndpoint();
@ -118,7 +119,7 @@ export const logoutUser = async () => {
clearKeys();
clearData();
try {
await caches.delete('thumbs');
await deleteThumbnailCache();
} catch (e) {
// ignore
}