Add config.yaml value for cards cache capacity.

373a0ad3211e30903668a49f7d49e6422200c59f

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

3 files changed, +9 -5Ignore whitespace
default/config.yaml+2 -0
@@ -1,6 +1,8 @@
11# -- DATA CONFIGURATION --
22# Root directory for user data storage
33dataRoot: ./data
4+# The maximum amount of memory that parsed character cards can use in MB
5+cardsCacheCapacity: 100
46# -- SERVER CONFIGURATION --
57# Listen for incoming connections
68listen: false
src/endpoints/characters.js+4 -3
@@ -14,7 +14,7 @@ import jimp from 'jimp';
1414
1515import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';
1616import { jsonParser, urlencodedParser } from '../express-common.js';
1717import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, MemoryLimitedMap, getConfigValue } from '../util.js';
1818import { TavernCardValidator } from '../validator/TavernCardValidator.js';
1919import { parse, write } from '../character-card-parser.js';
2020import { readWorldInfoFile } from './worldinfo.js';
@@ -23,8 +23,9 @@ import { importRisuSprites } from './sprites.js';
2323const defaultAvatarPath = './public/img/ai4.png';
2424
2525// KV-store for parsed character data
26-// 100 MB limit. Would take roughly 3000 characters to reach this limit
26+const cacheCapacity = Number(getConfigValue('cardsCacheCapacity', 100)); // MB
27-const characterDataCache = new MemoryLimitedMap(1024 * 1024 * 100);
27+// With 100 MB limit it would take roughly 3000 characters to reach this limit
28+const characterDataCache = new MemoryLimitedMap(1024 * 1024 * cacheCapacity);
2829// Some Android devices require tighter memory management
2930const isAndroid = process.platform === 'android';
3031
src/util.js+3 -2
@@ -680,8 +680,9 @@ export class MemoryLimitedMap {
680680 * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values.
681681 */
682682 constructor(maxMemoryInBytes) {
683683 if (typeof maxMemoryInBytes !== 'number' || maxMemoryInBytes <= 0 || isNaN(maxMemoryInBytes)) {
684- throw new Error('maxMemoryInBytes must be a positive number');
684+ console.warn('Invalid maxMemoryInBytes, using a fallback value of 1 GB.');
685+ maxMemoryInBytes = 1024 * 1024 * 1024; // 1 GB
685686 }
686687 this.maxMemory = maxMemoryInBytes;
687688 this.currentMemory = 0;