Add cache validation func to startup

This commit is contained in:
Cohee
2025-03-19 23:03:39 +02:00
parent 0e2290dacf
commit 9c7d3d7400
2 changed files with 30 additions and 0 deletions
+28
View File
@@ -54,6 +54,34 @@ const diskCache = {
},
};
/**
* Verifies disk cache size and prunes it if necessary.
* @param {import('../users.js').UserDirectoryList[]} directoriesList List of user directories
* @returns {Promise<void>}
*/
export async function verifyCharactersDiskCache(directoriesList) {
if (!useDiskCache) {
return;
}
const cache = await diskCache.instance();
const validKeys = [];
for (const dir of directoriesList) {
const files = fs.readdirSync(dir.characters);
for (const file of files) {
const filePath = path.join(dir.characters, file);
const stat = fs.statSync(filePath);
validKeys.push(`${filePath}-${stat.mtimeMs}`);
}
}
const cacheKeys = await cache.keys();
for (const key of cacheKeys) {
if (!validKeys.includes(key)) {
await cache.removeItem(key);
}
}
}
/**
* Reads the character card from the specified image file.
* @param {string} inputFile - Path to the image file