Add config, increase cache TTL, use async file reads

This commit is contained in:
Cohee
2025-03-15 19:43:26 +02:00
parent b8f8be8cf9
commit 684ee98168
3 changed files with 9 additions and 6 deletions

View File

@ -140,6 +140,8 @@ performance:
lazyLoadCharacters: false
# The maximum amount of memory that parsed character cards can use. Set to 0 to disable memory caching.
memoryCacheCapacity: '100mb'
# Enables disk caching for character cards. Improves performances with large card libraries.
useDiskCache: false
# Allow secret keys exposure via API
allowKeysExposure: false

View File

@ -81,14 +81,14 @@ export const read = (image) => {
* Parses a card image and returns the character metadata.
* @param {string} cardUrl Path to the card image
* @param {string} format File format
* @returns {string} Character data
* @returns {Promise<string>} Character data
*/
export const parse = (cardUrl, format) => {
export const parse = async (cardUrl, format) => {
let fileFormat = format === undefined ? 'png' : format;
switch (fileFormat) {
case 'png': {
const buffer = fs.readFileSync(cardUrl);
const buffer = await fs.promises.readFile(cardUrl);
return read(buffer);
}
}

View File

@ -30,7 +30,7 @@ const memoryCache = new MemoryLimitedMap(memoryCacheCapacity);
const isAndroid = process.platform === 'android';
// Use shallow character data for the character list
const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters', false, 'boolean');
const useDiskCache = true; // !!getConfigValue('performance.useDiskCache', false, 'boolean');
const useDiskCache = !!getConfigValue('performance.useDiskCache', false, 'boolean');
const diskCache = {
/**
@ -48,7 +48,8 @@ const diskCache = {
}
const cacheDir = path.join(globalThis.DATA_ROOT, '_cache', 'characters');
this._instance = storage.create({ dir: cacheDir, ttl: true });
const ttl = 7 * 24 * 60 * 60 * 1000; // 7 days
this._instance = storage.create({ dir: cacheDir, ttl: ttl });
await this._instance.init();
return this._instance;
},
@ -73,7 +74,7 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
}
}
const result = parse(inputFile, inputFormat);
const result = await parse(inputFile, inputFormat);
!isAndroid && memoryCache.set(cacheKey, result);
if (useDiskCache) {
await diskCache.instance().then(i => i.setItem(cacheKey, result));