Split accessing cache instance and processing data #3747

921850a62bfdaf3eceedc0cd4919786d555f04d2

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +23 -17Ignore whitespace
src/endpoints/characters.js+23 -17
@@ -115,24 +115,28 @@ class DiskCache {
115115 * @returns {Promise<void>}
116116 */
117117 async verify(directoriesList) {
118- if (!useDiskCache) {
118+ try {
119- return;
119+ if (!useDiskCache) {
120- }
120+ return;
121+ }
121122
122123 const cache = await this.instance();
123124 const validKeys = new Set();
124125 for (const dir of directoriesList) {
125126 const files = fs.readdirSync(dir.characters, { withFileTypes: true });
126127 for (const file of files.filter(f => f.isFile() && path.extname(f.name) === '.png')) {
127128 const filePath = path.join(dir.characters, file.name);
128129 const cacheKey = getCacheKey(filePath);
129130 validKeys.add(path.parse(cache.getDatumPath(cacheKey)).base);
131+ }
130132 }
131- }
133+ for (const key of this.hashedKeys) {
132- for (const key of this.hashedKeys) {
134+ if (!validKeys.has(key)) {
133- if (!validKeys.has(key)) {
135+ await cache.removeItem(key);
134- await cache.removeItem(key);
136+ }
135137 }
138+ } catch (error) {
139+ console.error('Error while verifying disk cache:', error);
136140 }
137141 }
138142
@@ -172,7 +176,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
172176 }
173177 if (useDiskCache) {
174178 try {
175179 const cachedDatacache = await diskCache.instance().then(i => i.getItem(cacheKey));
180+ const cachedData = await cache.getItem(cacheKey);
176181 if (cachedData) {
177182 return cachedData;
178183 }
@@ -185,7 +190,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
185190 !isAndroid && memoryCache.set(cacheKey, result);
186191 if (useDiskCache) {
187192 try {
188- await diskCache.instance().then(i => i.setItem(cacheKey, result));
193+ const cache = await diskCache.instance();
194+ await cache.setItem(cacheKey, result);
189195 } catch (error) {
190196 console.warn('Error while writing to disk cache:', error);
191197 }