From 87ac116b9b89e4756986b574f5eac0a76eac2ac9 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Sun, 7 Dec 2025 19:35:12 +0100 Subject: [PATCH] feat: implement recursive file deletion in ServerCacheService to remove matching JSON cache files based on prefix key --- src/lib/services/server-cache.service.ts | 45 +++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/lib/services/server-cache.service.ts b/src/lib/services/server-cache.service.ts index cecaed5..980d6ef 100644 --- a/src/lib/services/server-cache.service.ts +++ b/src/lib/services/server-cache.service.ts @@ -369,10 +369,47 @@ class ServerCacheService { } }); } else { - const cacheDir = path.join(this.cacheDir, prefixKey); - if (fs.existsSync(cacheDir)) { - fs.rmdirSync(cacheDir, { recursive: true }); - } + // En mode fichier, parcourir récursivement tous les fichiers et supprimer ceux qui correspondent + if (!fs.existsSync(this.cacheDir)) return; + + const deleteMatchingFiles = (dirPath: string): void => { + const items = fs.readdirSync(dirPath); + + for (const item of items) { + const itemPath = path.join(dirPath, item); + try { + const stats = fs.statSync(itemPath); + + if (stats.isDirectory()) { + deleteMatchingFiles(itemPath); + // Supprimer le répertoire s'il est vide après suppression des fichiers + try { + const remainingItems = fs.readdirSync(itemPath); + if (remainingItems.length === 0) { + fs.rmdirSync(itemPath); + } + } catch { + // Ignore les erreurs de suppression de répertoire + } + } else if (stats.isFile() && item.endsWith(".json")) { + // Extraire la clé du chemin relatif (sans l'extension .json) + const relativePath = path.relative(this.cacheDir, itemPath); + const key = relativePath.slice(0, -5).replace(/\\/g, "/"); // Remove .json and normalize slashes + + if (key.startsWith(prefixKey)) { + fs.unlinkSync(itemPath); + if (process.env.CACHE_DEBUG === "true") { + logger.debug(`🗑️ [CACHE DELETE] ${key}`); + } + } + } + } catch (error) { + logger.error({ err: error, path: itemPath }, `Could not delete cache file ${itemPath}`); + } + } + }; + + deleteMatchingFiles(this.cacheDir); } }