Split accessing cache instance and processing data

#3747
This commit is contained in:
Cohee
2025-03-23 23:09:21 +02:00
parent 4048a0a09a
commit 921850a62b

View File

@@ -115,24 +115,28 @@ class DiskCache {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async verify(directoriesList) { async verify(directoriesList) {
if (!useDiskCache) { try {
return; if (!useDiskCache) {
} return;
}
const cache = await this.instance(); const cache = await this.instance();
const validKeys = new Set(); const validKeys = new Set();
for (const dir of directoriesList) { for (const dir of directoriesList) {
const files = fs.readdirSync(dir.characters, { withFileTypes: true }); const files = fs.readdirSync(dir.characters, { withFileTypes: true });
for (const file of files.filter(f => f.isFile() && path.extname(f.name) === '.png')) { for (const file of files.filter(f => f.isFile() && path.extname(f.name) === '.png')) {
const filePath = path.join(dir.characters, file.name); const filePath = path.join(dir.characters, file.name);
const cacheKey = getCacheKey(filePath); const cacheKey = getCacheKey(filePath);
validKeys.add(path.parse(cache.getDatumPath(cacheKey)).base); validKeys.add(path.parse(cache.getDatumPath(cacheKey)).base);
}
} }
} for (const key of this.hashedKeys) {
for (const key of this.hashedKeys) { if (!validKeys.has(key)) {
if (!validKeys.has(key)) { await cache.removeItem(key);
await cache.removeItem(key); }
} }
} catch (error) {
console.error('Error while verifying disk cache:', error);
} }
} }
@@ -172,7 +176,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
} }
if (useDiskCache) { if (useDiskCache) {
try { try {
const cachedData = await diskCache.instance().then(i => i.getItem(cacheKey)); const cache = await diskCache.instance();
const cachedData = await cache.getItem(cacheKey);
if (cachedData) { if (cachedData) {
return cachedData; return cachedData;
} }
@@ -185,7 +190,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
!isAndroid && memoryCache.set(cacheKey, result); !isAndroid && memoryCache.set(cacheKey, result);
if (useDiskCache) { if (useDiskCache) {
try { try {
await diskCache.instance().then(i => i.setItem(cacheKey, result)); const cache = await diskCache.instance();
await cache.setItem(cacheKey, result);
} catch (error) { } catch (error) {
console.warn('Error while writing to disk cache:', error); console.warn('Error while writing to disk cache:', error);
} }