Add config, increase cache TTL, use async file reads
| @@ -140,6 +140,8 @@ performance: | |||
| 140 | lazyLoadCharacters: false | 140 | 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 | ||
| 143 | 145 | ||
| 144 | # Allow secret keys exposure via API | 146 | # Allow secret keys exposure via API |
| 145 | allowKeysExposure: false | 147 | allowKeysExposure: false |
| @@ -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 image | 82 | * @param {string} cardUrl Path to the card image |
| 83 | * @param {string} format File format | 83 | * @param {string} format File format |
| 84 | * @returns {string} Character data | 84 | * @returns {Promise<string>} Character data |
| 85 | */ | 85 | */ |
| 86 | export const parse = (cardUrl, format) => { | 86 | export const parse = async (cardUrl, format) => { |
| 87 | let fileFormat = format === undefined ? 'png' : format; | 87 | let fileFormat = format === undefined ? 'png' : format; |
| 88 | 88 | ||
| 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 | } |
| @@ -30,7 +30,7 @@ const memoryCache = new MemoryLimitedMap(memoryCacheCapacity); | |||
| 30 | const isAndroid = process.platform === 'android'; | 30 | const isAndroid = process.platform === 'android'; |
| 31 | // Use shallow character data for the character list | 31 | // Use shallow character data for the character list |
| 32 | const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean'); | 32 | const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean'); |
| 33 | const useDiskCache = true; // !!getConfigValue('performance.useDiskCache', false, 'boolean'); | 33 | const useDiskCache = !!getConfigValue('performance.useDiskCache', false, 'boolean'); |
| 34 | 34 | ||
| 35 | const diskCache = { | 35 | const diskCache = { |
| 36 | /** | 36 | /** |
| @@ -48,7 +48,8 @@ const diskCache = { | |||
| 48 | } | 48 | } |
| 49 | 49 | ||
| 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 | } |
| 75 | 76 | ||
| 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)); |