|
| 1 | +import path from 'node:path'; |
| 2 | +import { DriveFileItem, DriveFolderItem } from '../../types/drive.types'; |
| 3 | + |
| 4 | +type CacheEntry<T> = { |
| 5 | + value: T; |
| 6 | + expiresAt: number; |
| 7 | +}; |
| 8 | + |
| 9 | +export type WebDavFolderContent = { |
| 10 | + folders: DriveFolderItem[]; |
| 11 | + files: DriveFileItem[]; |
| 12 | +}; |
| 13 | + |
| 14 | +export class WebDavCacheService { |
| 15 | + public static readonly instance = new WebDavCacheService(); |
| 16 | + |
| 17 | + private static readonly TTL_MS = 10 * 60 * 1000; |
| 18 | + |
| 19 | + private readonly foldersByPath = new Map<string, CacheEntry<DriveFolderItem>>(); |
| 20 | + private readonly filesByPath = new Map<string, CacheEntry<DriveFileItem>>(); |
| 21 | + private readonly folderContentByPath = new Map<string, CacheEntry<WebDavFolderContent>>(); |
| 22 | + |
| 23 | + private readonly normalizeFolderPath = (folderPath: string): string => { |
| 24 | + let normalizedPath = folderPath || '/'; |
| 25 | + if (!normalizedPath.startsWith('/')) normalizedPath = `/${normalizedPath}`; |
| 26 | + if (!normalizedPath.endsWith('/')) normalizedPath = `${normalizedPath}/`; |
| 27 | + return normalizedPath; |
| 28 | + }; |
| 29 | + |
| 30 | + private readonly normalizeFilePath = (filePath: string): string => { |
| 31 | + const normalizedPath = filePath.startsWith('/') ? filePath : `/${filePath}`; |
| 32 | + return normalizedPath.length > 1 ? normalizedPath.replace(/\/$/, '') : normalizedPath; |
| 33 | + }; |
| 34 | + |
| 35 | + private readonly entry = <T>(value: T): CacheEntry<T> => ({ |
| 36 | + value, |
| 37 | + expiresAt: Date.now() + WebDavCacheService.TTL_MS, |
| 38 | + }); |
| 39 | + |
| 40 | + private readonly getFresh = <T>(map: Map<string, CacheEntry<T>>, key: string): T | undefined => { |
| 41 | + const cached = map.get(key); |
| 42 | + if (!cached) return; |
| 43 | + if (cached.expiresAt < Date.now()) { |
| 44 | + map.delete(key); |
| 45 | + return; |
| 46 | + } |
| 47 | + return cached.value; |
| 48 | + }; |
| 49 | + |
| 50 | + getFolder = (folderPath: string) => this.getFresh(this.foldersByPath, this.normalizeFolderPath(folderPath)); |
| 51 | + |
| 52 | + setFolder = (folderPath: string, folder: DriveFolderItem) => { |
| 53 | + this.foldersByPath.set(this.normalizeFolderPath(folderPath), this.entry(folder)); |
| 54 | + }; |
| 55 | + |
| 56 | + getFile = (filePath: string) => this.getFresh(this.filesByPath, this.normalizeFilePath(filePath)); |
| 57 | + |
| 58 | + setFile = (filePath: string, file: DriveFileItem) => { |
| 59 | + this.filesByPath.set(this.normalizeFilePath(filePath), this.entry(file)); |
| 60 | + }; |
| 61 | + |
| 62 | + getFolderContent = (folderPath: string) => |
| 63 | + this.getFresh(this.folderContentByPath, this.normalizeFolderPath(folderPath)); |
| 64 | + |
| 65 | + setFolderContent = (folderPath: string, folderContent: WebDavFolderContent) => { |
| 66 | + this.folderContentByPath.set(this.normalizeFolderPath(folderPath), this.entry(folderContent)); |
| 67 | + }; |
| 68 | + |
| 69 | + invalidateResource = (resourcePath: string) => { |
| 70 | + const filePath = this.normalizeFilePath(resourcePath); |
| 71 | + const folderPath = this.normalizeFolderPath(resourcePath); |
| 72 | + const parentFolderPath = this.normalizeFolderPath(path.posix.dirname(filePath)); |
| 73 | + |
| 74 | + this.filesByPath.delete(filePath); |
| 75 | + this.foldersByPath.delete(folderPath); |
| 76 | + this.folderContentByPath.delete(folderPath); |
| 77 | + this.folderContentByPath.delete(parentFolderPath); |
| 78 | + }; |
| 79 | + |
| 80 | + clear = () => { |
| 81 | + this.foldersByPath.clear(); |
| 82 | + this.filesByPath.clear(); |
| 83 | + this.folderContentByPath.clear(); |
| 84 | + }; |
| 85 | +} |
0 commit comments