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:
140 lazyLoadCharacters: false140 lazyLoadCharacters: false
141 # The maximum amount of memory that parsed character cards can use. Set to 0 to disable memory caching.141 # The maximum amount of memory that parsed character cards can use. Set to 0 to disable memory caching.
142 memoryCacheCapacity: '100mb'142 memoryCacheCapacity: '100mb'
143 # Enables disk caching for character cards. Improves performances with large card libraries.
144 useDiskCache: false
143145
144# Allow secret keys exposure via API146# Allow secret keys exposure via API
145allowKeysExposure: false147allowKeysExposure: false
src/character-card-parser.js+3 -3
@@ -81,14 +81,14 @@ export const read = (image) => {
81 * Parses a card image and returns the character metadata.81 * Parses a card image and returns the character metadata.
82 * @param {string} cardUrl Path to the card image82 * @param {string} cardUrl Path to the card image
83 * @param {string} format File format83 * @param {string} format File format
84 * @returns {string} Character data84 * @returns {Promise<string>} Character data
85 */85 */
86export const parse = (cardUrl, format) => {86export const parse = async (cardUrl, format) => {
87 let fileFormat = format === undefined ? 'png' : format;87 let fileFormat = format === undefined ? 'png' : format;
8888
89 switch (fileFormat) {89 switch (fileFormat) {
90 case 'png': {90 case 'png': {
91 const buffer = fs.readFileSync(cardUrl);91 const buffer = await fs.promises.readFile(cardUrl);
92 return read(buffer);92 return read(buffer);
93 }93 }
94 }94 }
src/endpoints/characters.js+4 -3
@@ -30,7 +30,7 @@ const memoryCache = new MemoryLimitedMap(memoryCacheCapacity);
30const isAndroid = process.platform === 'android';30const isAndroid = process.platform === 'android';
31// Use shallow character data for the character list31// Use shallow character data for the character list
32const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean');32const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean');
33const useDiskCache = true; // !!getConfigValue('performance.useDiskCache', false, 'boolean');33const useDiskCache = !!getConfigValue('performance.useDiskCache', false, 'boolean');
3434
35const diskCache = {35const diskCache = {
36 /**36 /**
@@ -48,7 +48,8 @@ const diskCache = {
48 }48 }
4949
50 const cacheDir = path.join(globalThis.DATA_ROOT, '_cache', 'characters');50 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 });
52 await this._instance.init();53 await this._instance.init();
53 return this._instance;54 return this._instance;
54 },55 },
@@ -73,7 +74,7 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
73 }74 }
74 }75 }
7576
76 const result = parse(inputFile, inputFormat);77 const result = await parse(inputFile, inputFormat);
77 !isAndroid && memoryCache.set(cacheKey, result);78 !isAndroid && memoryCache.set(cacheKey, result);
78 if (useDiskCache) {79 if (useDiskCache) {
79 await diskCache.instance().then(i => i.setItem(cacheKey, result));80 await diskCache.instance().then(i => i.setItem(cacheKey, result));