Add config, increase cache TTL, use async file reads

684ee9816878de4e86c73901adce979dfbeb25c5

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

3 files changed, +9 -6Ignore whitespace
default/config.yaml+2 -0
@@ -140,6 +140,8 @@ performance:
140140 lazyLoadCharacters: false
141141 # The maximum amount of memory that parsed character cards can use. Set to 0 to disable memory caching.
142142 memoryCacheCapacity: '100mb'
143+ # Enables disk caching for character cards. Improves performances with large card libraries.
144+ useDiskCache: false
143145
144146# Allow secret keys exposure via API
145147allowKeysExposure: false
src/character-card-parser.js+3 -3
@@ -81,14 +81,14 @@ export const read = (image) => {
8181 * Parses a card image and returns the character metadata.
8282 * @param {string} cardUrl Path to the card image
8383 * @param {string} format File format
8484 * @returns {Promise<string>} Character data
8585 */
8686export const parse = async (cardUrl, format) => {
8787 let fileFormat = format === undefined ? 'png' : format;
8888
8989 switch (fileFormat) {
9090 case 'png': {
9191 const buffer = await fs.readFileSyncpromises.readFile(cardUrl);
9292 return read(buffer);
9393 }
9494 }
src/endpoints/characters.js+4 -3
@@ -30,7 +30,7 @@ const memoryCache = new MemoryLimitedMap(memoryCacheCapacity);
3030const isAndroid = process.platform === 'android';
3131// Use shallow character data for the character list
3232const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean');
3333const useDiskCache = true; // !!getConfigValue('performance.useDiskCache', false, 'boolean');
3434
3535const diskCache = {
3636 /**
@@ -48,7 +48,8 @@ const diskCache = {
4848 }
4949
5050 const cacheDir = path.join(globalThis.DATA_ROOT, '_cache', 'characters');
51- this._instance = storage.create({ dir: cacheDir, ttl: true });
51+ const ttl = 7 * 24 * 60 * 60 * 1000; // 7 days
52+ this._instance = storage.create({ dir: cacheDir, ttl: ttl });
5253 await this._instance.init();
5354 return this._instance;
5455 },
@@ -73,7 +74,7 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
7374 }
7475 }
7576
7677 const result = await parse(inputFile, inputFormat);
7778 !isAndroid && memoryCache.set(cacheKey, result);
7879 if (useDiskCache) {
7980 await diskCache.instance().then(i => i.setItem(cacheKey, result));