From 8be08882d88676b5abc67a80bc169dd63e42668b Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Mon, 19 Aug 2024 19:23:41 -0700 Subject: [PATCH 1/3] Update formatting in CacheManager --- server/managers/CacheManager.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index c273d389..24d43b67 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -16,7 +16,8 @@ class CacheManager { /** * Create cache directory paths if they dont exist */ - async ensureCachePaths() { // Creates cache paths if necessary and sets owner and permissions + async ensureCachePaths() { + // Creates cache paths if necessary and sets owner and permissions this.CachePath = Path.join(global.MetadataPath, 'cache') this.CoverCachePath = Path.join(this.CachePath, 'covers') this.ImageCachePath = Path.join(this.CachePath, 'images') @@ -89,23 +90,28 @@ class CacheManager { } async purgeEntityCache(entityId, cachePath) { - return Promise.all((await fs.readdir(cachePath)).reduce((promises, file) => { - if (file.startsWith(entityId)) { - Logger.debug(`[CacheManager] Going to purge ${file}`); - promises.push(this.removeCache(Path.join(cachePath, file))) - } - return promises - }, [])) + return Promise.all( + (await fs.readdir(cachePath)).reduce((promises, file) => { + if (file.startsWith(entityId)) { + Logger.debug(`[CacheManager] Going to purge ${file}`) + promises.push(this.removeCache(Path.join(cachePath, file))) + } + return promises + }, []) + ) } removeCache(path) { if (!path) return false return fs.pathExists(path).then((exists) => { if (!exists) return false - return fs.unlink(path).then(() => true).catch((err) => { - Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err) - return false - }) + return fs + .unlink(path) + .then(() => true) + .catch((err) => { + Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err) + return false + }) }) } @@ -158,4 +164,4 @@ class CacheManager { readStream.pipe(res) } } -module.exports = new CacheManager() \ No newline at end of file +module.exports = new CacheManager() From ccdc3d60c444d0526d3b31d9e28d73536925ec91 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Mon, 19 Aug 2024 19:25:01 -0700 Subject: [PATCH 2/3] Change: CacheManager use ensureDir --- server/managers/CacheManager.js | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index 24d43b67..8f810a33 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -23,21 +23,10 @@ class CacheManager { this.ImageCachePath = Path.join(this.CachePath, 'images') this.ItemCachePath = Path.join(this.CachePath, 'items') - if (!(await fs.pathExists(this.CachePath))) { - await fs.mkdir(this.CachePath) - } - - if (!(await fs.pathExists(this.CoverCachePath))) { - await fs.mkdir(this.CoverCachePath) - } - - if (!(await fs.pathExists(this.ImageCachePath))) { - await fs.mkdir(this.ImageCachePath) - } - - if (!(await fs.pathExists(this.ItemCachePath))) { - await fs.mkdir(this.ItemCachePath) - } + await fs.ensureDir(this.CachePath) + await fs.ensureDir(this.CoverCachePath) + await fs.ensureDir(this.ImageCachePath) + await fs.ensureDir(this.ItemCachePath) } async handleCoverCache(res, libraryItemId, coverPath, options = {}) { From 996c78d760b25574711930cada06bf2b20b75f2d Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Mon, 19 Aug 2024 19:32:53 -0700 Subject: [PATCH 3/3] Add: clear metadata cache when restoring backup --- server/managers/BackupManager.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/managers/BackupManager.js b/server/managers/BackupManager.js index 71b8304c..54727b6b 100644 --- a/server/managers/BackupManager.js +++ b/server/managers/BackupManager.js @@ -14,6 +14,7 @@ const fileUtils = require('../utils/fileUtils') const { getFileSize } = require('../utils/fileUtils') const Backup = require('../objects/Backup') +const CacheManager = require('./CacheManager') class BackupManager { constructor(notificationManager) { @@ -230,6 +231,9 @@ class BackupManager { // Reset api cache, set hooks again await apiCacheManager.reset() + // Clear metadata cache + await CacheManager.purgeAll() + res.sendStatus(200) // Triggers browser refresh for all clients