Use human-readable memory cache capacity in config

28bad6479cafba8fa1effe94b30528e26b26518b

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

4 files changed, +19 -19Ignore whitespace
default/config.yaml+2 -2
@@ -138,8 +138,8 @@ performance:
138138 # Enables lazy loading of character cards. Improves performances with large card libraries.
139139 # May have compatibility issues with some extensions.
140140 lazyLoadCharacters: false
141141 # The maximum amount of memory that parsed character cards can use. inSet MBto 0 to disable memory caching.
142142 cardsCacheCapacitymemoryCacheCapacity: 100'100mb'
143143
144144# Allow secret keys exposure via API
145145allowKeysExposure: false
post-install.js+2 -2
@@ -98,8 +98,8 @@ const keyMigrationMap = [
9898 },
9999 {
100100 oldKey: 'cardsCacheCapacity',
101101 newKey: 'performance.cardsCacheCapacitymemoryCacheCapacity',
102102 migrate: (value) => `${value}mb`,
103103 },
104104 // uncomment one release after 1.12.13
105105 /*
src/endpoints/characters.js+7 -8
@@ -23,10 +23,9 @@ import { invalidateThumbnail } from './thumbnails.js';
2323import { importRisuSprites } from './sprites.js';
2424const defaultAvatarPath = './public/img/ai4.png';
2525
26-// KV-store for parsed character data
27-const cacheCapacity = Number(getConfigValue('performance.cardsCacheCapacity', 100, 'number')); // MB
2826// With 100 MB limit it would take roughly 3000 characters to reach this limit
29-const characterDataCache = new MemoryLimitedMap(1024 * 1024 * cacheCapacity);
27+const memoryCacheCapacity = getConfigValue('performance.memoryCacheCapacity', '100mb');
28+const memoryCache = new MemoryLimitedMap(memoryCacheCapacity);
3029// Some Android devices require tighter memory management
3130const isAndroid = process.platform === 'android';
3231// Use shallow character data for the character list
@@ -41,12 +40,12 @@ const useShallowCharacters = !!getConfigValue('performance.lazyLoadCharacters',
4140async function readCharacterData(inputFile, inputFormat = 'png') {
4241 const stat = fs.statSync(inputFile);
4342 const cacheKey = `${inputFile}-${stat.mtimeMs}`;
4443 if (characterDataCachememoryCache.has(cacheKey)) {
4544 return characterDataCachememoryCache.get(cacheKey);
4645 }
4746
4847 const result = parse(inputFile, inputFormat);
4948 !isAndroid && characterDataCachememoryCache.set(cacheKey, result);
5049 return result;
5150}
5251
@@ -62,12 +61,12 @@ async function readCharacterData(inputFile, inputFormat = 'png') {
6261async function writeCharacterData(inputFile, data, outputFile, request, crop = undefined) {
6362 try {
6463 // Reset the cache
6564 for (const key of characterDataCachememoryCache.keys()) {
6665 if (Buffer.isBuffer(inputFile)) {
6766 break;
6867 }
6968 if (key.startsWith(inputFile)) {
7069 characterDataCachememoryCache.delete(key);
7170 break;
7271 }
7372 }
src/util.js+8 -7
@@ -16,6 +16,7 @@ import mime from 'mime-types';
1616import { default as simpleGit } from 'simple-git';
1717import chalk from 'chalk';
1818import { LOG_LEVELS } from './constants.js';
19+import bytes from 'bytes';
1920
2021/**
2122 * Parsed config object.
@@ -856,14 +857,10 @@ export function setupLogLevel() {
856857export class MemoryLimitedMap {
857858 /**
858859 * Creates an instance of MemoryLimitedMap.
859860 * @param {numberstring} maxMemoryInBytescacheCapacity - The maximum allowedMaximum memory usage in byteshuman-readable forformat string(e.g., values'1 GB').
860861 */
861862 constructor(maxMemoryInBytescacheCapacity) {
862- if (typeof maxMemoryInBytes !== 'number' || maxMemoryInBytes <= 0 || isNaN(maxMemoryInBytes)) {
863+ this.maxMemory = bytes.parse(cacheCapacity) ?? 0;
863- console.warn('Invalid maxMemoryInBytes, using a fallback value of 1 GB.');
864- maxMemoryInBytes = 1024 * 1024 * 1024; // 1 GB
865- }
866- this.maxMemory = maxMemoryInBytes;
867864 this.currentMemory = 0;
868865 this.map = new Map();
869866 this.queue = [];
@@ -886,6 +883,10 @@ export class MemoryLimitedMap {
886883 * @param {string} value
887884 */
888885 set(key, value) {
886+ if (this.maxMemory <= 0) {
887+ return;
888+ }
889+
889890 if (typeof key !== 'string' || typeof value !== 'string') {
890891 return;
891892 }