Rename LimitedMap => MemoryLimitedMap

This commit is contained in:
Cohee
2024-11-29 12:33:48 +00:00
parent 176ef77624
commit 095d19cda7
2 changed files with 10 additions and 10 deletions

View File

@ -14,7 +14,7 @@ import jimp from 'jimp';
import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';
import { jsonParser, urlencodedParser } from '../express-common.js'; import { jsonParser, urlencodedParser } from '../express-common.js';
import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, LimitedMap } from '../util.js'; import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, MemoryLimitedMap } from '../util.js';
import { TavernCardValidator } from '../validator/TavernCardValidator.js'; import { TavernCardValidator } from '../validator/TavernCardValidator.js';
import { parse, write } from '../character-card-parser.js'; import { parse, write } from '../character-card-parser.js';
import { readWorldInfoFile } from './worldinfo.js'; import { readWorldInfoFile } from './worldinfo.js';
@ -24,7 +24,7 @@ const defaultAvatarPath = './public/img/ai4.png';
// KV-store for parsed character data // KV-store for parsed character data
// 100 MB limit. Would take roughly 3000 characters to reach this limit // 100 MB limit. Would take roughly 3000 characters to reach this limit
const characterDataCache = new LimitedMap(1024 * 1024 * 100); const characterDataCache = new MemoryLimitedMap(1024 * 1024 * 100);
// Some Android devices require tighter memory management // Some Android devices require tighter memory management
const isAndroid = process.platform === 'android'; const isAndroid = process.platform === 'android';

View File

@ -672,11 +672,11 @@ export function isValidUrl(url) {
} }
/** /**
* LimitedMap class that limits the memory usage of string values. * MemoryLimitedMap class that limits the memory usage of string values.
*/ */
export class LimitedMap { export class MemoryLimitedMap {
/** /**
* Creates an instance of LimitedMap. * Creates an instance of MemoryLimitedMap.
* @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values. * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values.
*/ */
constructor(maxMemoryInBytes) { constructor(maxMemoryInBytes) {
@ -710,7 +710,7 @@ export class LimitedMap {
return; return;
} }
const newValueSize = LimitedMap.estimateStringSize(value); const newValueSize = MemoryLimitedMap.estimateStringSize(value);
// If the new value itself exceeds the max memory, reject it // If the new value itself exceeds the max memory, reject it
if (newValueSize > this.maxMemory) { if (newValueSize > this.maxMemory) {
@ -720,7 +720,7 @@ export class LimitedMap {
// Check if the key already exists to adjust memory accordingly // Check if the key already exists to adjust memory accordingly
if (this.map.has(key)) { if (this.map.has(key)) {
const oldValue = this.map.get(key); const oldValue = this.map.get(key);
const oldValueSize = LimitedMap.estimateStringSize(oldValue); const oldValueSize = MemoryLimitedMap.estimateStringSize(oldValue);
this.currentMemory -= oldValueSize; this.currentMemory -= oldValueSize;
// Remove the key from its current position in the queue // Remove the key from its current position in the queue
const index = this.queue.indexOf(key); const index = this.queue.indexOf(key);
@ -733,7 +733,7 @@ export class LimitedMap {
while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) { while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) {
const oldestKey = this.queue.shift(); const oldestKey = this.queue.shift();
const oldestValue = this.map.get(oldestKey); const oldestValue = this.map.get(oldestKey);
const oldestValueSize = LimitedMap.estimateStringSize(oldestValue); const oldestValueSize = MemoryLimitedMap.estimateStringSize(oldestValue);
this.map.delete(oldestKey); this.map.delete(oldestKey);
this.currentMemory -= oldestValueSize; this.currentMemory -= oldestValueSize;
} }
@ -777,7 +777,7 @@ export class LimitedMap {
return false; return false;
} }
const value = this.map.get(key); const value = this.map.get(key);
const valueSize = LimitedMap.estimateStringSize(value); const valueSize = MemoryLimitedMap.estimateStringSize(value);
this.map.delete(key); this.map.delete(key);
this.currentMemory -= valueSize; this.currentMemory -= valueSize;
@ -842,7 +842,7 @@ export class LimitedMap {
} }
/** /**
* Makes the LimitedMap iterable. * Makes the MemoryLimitedMap iterable.
* @returns {Iterator} - Iterator over [key, value] pairs. * @returns {Iterator} - Iterator over [key, value] pairs.
*/ */
[Symbol.iterator]() { [Symbol.iterator]() {