Merge pull request #3311 from nichwall/backup_restore_clear_cache

Backup restore clear cache
This commit is contained in:
advplyr 2024-08-21 17:38:16 -05:00 committed by GitHub
commit 5f572face5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 28 deletions

View File

@ -14,6 +14,7 @@ const fileUtils = require('../utils/fileUtils')
const { getFileSize } = require('../utils/fileUtils') const { getFileSize } = require('../utils/fileUtils')
const Backup = require('../objects/Backup') const Backup = require('../objects/Backup')
const CacheManager = require('./CacheManager')
class BackupManager { class BackupManager {
constructor(notificationManager) { constructor(notificationManager) {
@ -230,6 +231,9 @@ class BackupManager {
// Reset api cache, set hooks again // Reset api cache, set hooks again
await apiCacheManager.reset() await apiCacheManager.reset()
// Clear metadata cache
await CacheManager.purgeAll()
res.sendStatus(200) res.sendStatus(200)
// Triggers browser refresh for all clients // Triggers browser refresh for all clients

View File

@ -16,27 +16,17 @@ class CacheManager {
/** /**
* Create cache directory paths if they dont exist * 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.CachePath = Path.join(global.MetadataPath, 'cache')
this.CoverCachePath = Path.join(this.CachePath, 'covers') this.CoverCachePath = Path.join(this.CachePath, 'covers')
this.ImageCachePath = Path.join(this.CachePath, 'images') this.ImageCachePath = Path.join(this.CachePath, 'images')
this.ItemCachePath = Path.join(this.CachePath, 'items') this.ItemCachePath = Path.join(this.CachePath, 'items')
if (!(await fs.pathExists(this.CachePath))) { await fs.ensureDir(this.CachePath)
await fs.mkdir(this.CachePath) await fs.ensureDir(this.CoverCachePath)
} await fs.ensureDir(this.ImageCachePath)
await fs.ensureDir(this.ItemCachePath)
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)
}
} }
async handleCoverCache(res, libraryItemId, coverPath, options = {}) { async handleCoverCache(res, libraryItemId, coverPath, options = {}) {
@ -89,23 +79,28 @@ class CacheManager {
} }
async purgeEntityCache(entityId, cachePath) { async purgeEntityCache(entityId, cachePath) {
return Promise.all((await fs.readdir(cachePath)).reduce((promises, file) => { return Promise.all(
if (file.startsWith(entityId)) { (await fs.readdir(cachePath)).reduce((promises, file) => {
Logger.debug(`[CacheManager] Going to purge ${file}`); if (file.startsWith(entityId)) {
promises.push(this.removeCache(Path.join(cachePath, file))) Logger.debug(`[CacheManager] Going to purge ${file}`)
} promises.push(this.removeCache(Path.join(cachePath, file)))
return promises }
}, [])) return promises
}, [])
)
} }
removeCache(path) { removeCache(path) {
if (!path) return false if (!path) return false
return fs.pathExists(path).then((exists) => { return fs.pathExists(path).then((exists) => {
if (!exists) return false if (!exists) return false
return fs.unlink(path).then(() => true).catch((err) => { return fs
Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err) .unlink(path)
return false .then(() => true)
}) .catch((err) => {
Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err)
return false
})
}) })
} }
@ -158,4 +153,4 @@ class CacheManager {
readStream.pipe(res) readStream.pipe(res)
} }
} }
module.exports = new CacheManager() module.exports = new CacheManager()