Skip to content

Commit

Permalink
Merge pull request #3311 from nichwall/backup_restore_clear_cache
Browse files Browse the repository at this point in the history
Backup restore clear cache
  • Loading branch information
advplyr authored Aug 21, 2024
2 parents 88a4cf9 + 996c78d commit 5f572fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
4 changes: 4 additions & 0 deletions server/managers/BackupManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
51 changes: 23 additions & 28 deletions server/managers/CacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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
})
})
}

Expand Down Expand Up @@ -158,4 +153,4 @@ class CacheManager {
readStream.pipe(res)
}
}
module.exports = new CacheManager()
module.exports = new CacheManager()

0 comments on commit 5f572fa

Please sign in to comment.