mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 15:08:46 +00:00
캐시 삭제 구현
This commit is contained in:
@@ -22,3 +22,7 @@ export const getFileCacheIndex = async () => {
|
||||
export const storeFileCacheIndex = async (fileCacheIndex: FileCacheIndex) => {
|
||||
await cacheIndex.fileCache.put(fileCacheIndex);
|
||||
};
|
||||
|
||||
export const deleteFileCacheIndex = async (fileId: number) => {
|
||||
await cacheIndex.fileCache.delete(fileId);
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user