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 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

View File

@ -16,27 +16,17 @@ 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')
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 = {}) {
@ -89,23 +79,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 +153,4 @@ class CacheManager {
readStream.pipe(res)
}
}
module.exports = new CacheManager()
module.exports = new CacheManager()