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 {
115 * @returns {Promise<void>}115 * @returns {Promise<void>}
116 */116 */
117 async verify(directoriesList) {117 async verify(directoriesList) {
118 if (!useDiskCache) {118 try {
119 return;119 if (!useDiskCache) {
120 }120 return;
121 }
121122
122 const cache = await this.instance();123 const cache = await this.instance();
123 const validKeys = new Set();124 const validKeys = new Set();
124 for (const dir of directoriesList) {125 for (const dir of directoriesList) {
125 const files = fs.readdirSync(dir.characters, { withFileTypes: true });126 const files = fs.readdirSync(dir.characters, { withFileTypes: true });
126 for (const file of files.filter(f => f.isFile() && path.extname(f.name) === '.png')) {127 for (const file of files.filter(f => f.isFile() && path.extname(f.name) === '.png')) {
127 const filePath = path.join(dir.characters, file.name);128 const filePath = path.join(dir.characters, file.name);
128 const cacheKey = getCacheKey(filePath);129 const cacheKey = getCacheKey(filePath);
129 validKeys.add(path.parse(cache.getDatumPath(cacheKey)).base);130 validKeys.add(path.parse(cache.getDatumPath(cacheKey)).base);
131 }
130 }132 }
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 }
135 }137 }
138 } catch (error) {
139 console.error('Error while verifying disk cache:', error);
136 }140 }
137 }141 }
138142
@@ -172,7 +176,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
172 }176 }
173 if (useDiskCache) {177 if (useDiskCache) {
174 try {178 try {
175 const cachedData = await diskCache.instance().then(i => i.getItem(cacheKey));179 const cache = await diskCache.instance();
180 const cachedData = await cache.getItem(cacheKey);
176 if (cachedData) {181 if (cachedData) {
177 return cachedData;182 return cachedData;
178 }183 }
@@ -185,7 +190,8 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
185 !isAndroid && memoryCache.set(cacheKey, result);190 !isAndroid && memoryCache.set(cacheKey, result);
186 if (useDiskCache) {191 if (useDiskCache) {
187 try {192 try {
188 await diskCache.instance().then(i => i.setItem(cacheKey, result));193 const cache = await diskCache.instance();
194 await cache.setItem(cacheKey, result);
189 } catch (error) {195 } catch (error) {
190 console.warn('Error while writing to disk cache:', error);196 console.warn('Error while writing to disk cache:', error);
191 }197 }