캐시 삭제 구현

This commit is contained in:
static
2025-01-14 03:26:32 +09:00
parent f37df53991
commit 27d2b83464
7 changed files with 55 additions and 19 deletions

View File

@@ -1,9 +1,10 @@
import {
getFileCacheIndex as getFileCacheIndexFromIndexedDB,
storeFileCacheIndex as storeFileCacheIndexToIndexedDB,
storeFileCacheIndex,
deleteFileCacheIndex,
type FileCacheIndex,
} from "$lib/indexedDB";
import { readFileFromOpfs, writeFileToOpfs } from "$lib/modules/opfs";
import { readFile, writeFile, deleteFile } from "$lib/modules/opfs";
const fileCacheIndex = new Map<number, FileCacheIndex>();
@@ -22,13 +23,13 @@ export const getFileCache = async (fileId: number) => {
if (!cacheIndex) return null;
cacheIndex.lastRetrievedAt = new Date();
storeFileCacheIndexToIndexedDB(cacheIndex); // Intended
return await readFileFromOpfs(`/cache/${fileId}`);
storeFileCacheIndex(cacheIndex); // Intended
return await readFile(`/cache/${fileId}`);
};
export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) => {
const now = new Date();
await writeFileToOpfs(`/cache/${fileId}`, fileBuffer);
await writeFile(`/cache/${fileId}`, fileBuffer);
const cacheIndex: FileCacheIndex = {
fileId,
@@ -37,5 +38,11 @@ export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) =>
size: fileBuffer.byteLength,
};
fileCacheIndex.set(fileId, cacheIndex);
await storeFileCacheIndexToIndexedDB(cacheIndex);
await storeFileCacheIndex(cacheIndex);
};
export const deleteFileCache = async (fileId: number) => {
await deleteFile(`/cache/${fileId}`);
fileCacheIndex.delete(fileId);
await deleteFileCacheIndex(fileId);
};

View File

@@ -18,31 +18,32 @@ const getFileHandle = async (path: string, create = true) => {
try {
let directoryHandle: FileSystemDirectoryHandle = rootHandle;
for (const part of parts.slice(0, -1)) {
if (!part) continue;
directoryHandle = await directoryHandle.getDirectoryHandle(part, { create });
}
return directoryHandle.getFileHandle(parts[parts.length - 1]!, { create });
const filename = parts[parts.length - 1]!;
const fileHandle = await directoryHandle.getFileHandle(filename, { create });
return { parentHandle: directoryHandle, filename, fileHandle };
} catch (e) {
if (e instanceof DOMException && e.name === "NotFoundError") {
return null;
return {};
}
throw e;
}
};
export const readFileFromOpfs = async (path: string) => {
const fileHandle = await getFileHandle(path, false);
export const readFile = async (path: string) => {
const { fileHandle } = await getFileHandle(path, false);
if (!fileHandle) return null;
const file = await fileHandle.getFile();
return await file.arrayBuffer();
};
export const writeFileToOpfs = async (path: string, data: ArrayBuffer) => {
const fileHandle = await getFileHandle(path);
export const writeFile = async (path: string, data: ArrayBuffer) => {
const { fileHandle } = await getFileHandle(path);
const writable = await fileHandle!.createWritable();
try {
@@ -51,3 +52,10 @@ export const writeFileToOpfs = async (path: string, data: ArrayBuffer) => {
await writable.close();
}
};
export const deleteFile = async (path: string) => {
const { parentHandle, filename } = await getFileHandle(path, false);
if (!parentHandle) return;
await parentHandle.removeEntry(filename);
};