Add config, increase cache TTL, use async file reads
| @@ -140,6 +140,8 @@ performance: | ||
| 140 | 140 | lazyLoadCharacters: false |
| 141 | 141 | # The maximum amount of memory that parsed character cards can use. Set to 0 to disable memory caching. |
| 142 | 142 | memoryCacheCapacity: '100mb' |
| 143 | + # Enables disk caching for character cards. Improves performances with large card libraries. | |
| 144 | + useDiskCache: false | |
| 143 | 145 | |
| 144 | 146 | # Allow secret keys exposure via API |
| 145 | 147 | allowKeysExposure: false |
| @@ -81,14 +81,14 @@ export const read = (image) => { | ||
| 81 | 81 | * Parses a card image and returns the character metadata. |
| 82 | 82 | * @param {string} cardUrl Path to the card image |
| 83 | 83 | * @param {string} format File format |
| 84 | 84 | * @returns {Promise<string>} Character data |
| 85 | 85 | */ |
| 86 | 86 | export const parse = async (cardUrl, format) => { |
| 87 | 87 | let fileFormat = format === undefined ? 'png' : format; |
| 88 | 88 | |
| 89 | 89 | switch (fileFormat) { |
| 90 | 90 | case 'png': { |
| 91 | 91 | const buffer = await fs.readFileSyncpromises.readFile(cardUrl); |
| 92 | 92 | return read(buffer); |
| 93 | 93 | } |
| 94 | 94 | } |
| @@ -30,7 +30,7 @@ const memoryCache = new MemoryLimitedMap(memoryCacheCapacity); | ||
| 30 | 30 | const isAndroid = process.platform === 'android'; |
| 31 | 31 | // Use shallow character data for the character list |
| 32 | 32 | const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean'); |
| 33 | 33 | const useDiskCache = true; // !!getConfigValue('performance.useDiskCache', false, 'boolean'); |
| 34 | 34 | |
| 35 | 35 | const diskCache = { |
| 36 | 36 | /** |
| @@ -48,7 +48,8 @@ const diskCache = { | ||
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 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 | 53 | await this._instance.init(); |
| 53 | 54 | return this._instance; |
| 54 | 55 | }, |
| @@ -73,7 +74,7 @@ async function readCharacterData(inputFile, inputFormat = 'png') { | ||
| 73 | 74 | } |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | const result = await parse(inputFile, inputFormat); |
| 77 | 78 | !isAndroid && memoryCache.set(cacheKey, result); |
| 78 | 79 | if (useDiskCache) { |
| 79 | 80 | await diskCache.instance().then(i => i.setItem(cacheKey, result)); |