Rename LimitedMap => MemoryLimitedMap
| @@ -14,7 +14,7 @@ import jimp from 'jimp'; | ||
| 14 | 14 | |
| 15 | 15 | import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; |
| 16 | 16 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 17 | 17 | import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, LimitedMapMemoryLimitedMap } from '../util.js'; |
| 18 | 18 | import { TavernCardValidator } from '../validator/TavernCardValidator.js'; |
| 19 | 19 | import { parse, write } from '../character-card-parser.js'; |
| 20 | 20 | import { readWorldInfoFile } from './worldinfo.js'; |
| @@ -24,7 +24,7 @@ const defaultAvatarPath = './public/img/ai4.png'; | ||
| 24 | 24 | |
| 25 | 25 | // KV-store for parsed character data |
| 26 | 26 | // 100 MB limit. Would take roughly 3000 characters to reach this limit |
| 27 | 27 | const characterDataCache = new LimitedMapMemoryLimitedMap(1024 * 1024 * 100); |
| 28 | 28 | // Some Android devices require tighter memory management |
| 29 | 29 | const isAndroid = process.platform === 'android'; |
| 30 | 30 | |
| @@ -672,11 +672,11 @@ export function isValidUrl(url) { | ||
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | /** |
| 675 | 675 | * LimitedMapMemoryLimitedMap class that limits the memory usage of string values. |
| 676 | 676 | */ |
| 677 | 677 | export class LimitedMapMemoryLimitedMap { |
| 678 | 678 | /** |
| 679 | 679 | * Creates an instance of LimitedMapMemoryLimitedMap. |
| 680 | 680 | * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values. |
| 681 | 681 | */ |
| 682 | 682 | constructor(maxMemoryInBytes) { |
| @@ -710,7 +710,7 @@ export class LimitedMap { | ||
| 710 | 710 | return; |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | const newValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(value); |
| 714 | 714 | |
| 715 | 715 | // If the new value itself exceeds the max memory, reject it |
| 716 | 716 | if (newValueSize > this.maxMemory) { |
| @@ -720,7 +720,7 @@ export class LimitedMap { | ||
| 720 | 720 | // Check if the key already exists to adjust memory accordingly |
| 721 | 721 | if (this.map.has(key)) { |
| 722 | 722 | const oldValue = this.map.get(key); |
| 723 | 723 | const oldValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(oldValue); |
| 724 | 724 | this.currentMemory -= oldValueSize; |
| 725 | 725 | // Remove the key from its current position in the queue |
| 726 | 726 | const index = this.queue.indexOf(key); |
| @@ -733,7 +733,7 @@ export class LimitedMap { | ||
| 733 | 733 | while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) { |
| 734 | 734 | const oldestKey = this.queue.shift(); |
| 735 | 735 | const oldestValue = this.map.get(oldestKey); |
| 736 | 736 | const oldestValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(oldestValue); |
| 737 | 737 | this.map.delete(oldestKey); |
| 738 | 738 | this.currentMemory -= oldestValueSize; |
| 739 | 739 | } |
| @@ -777,7 +777,7 @@ export class LimitedMap { | ||
| 777 | 777 | return false; |
| 778 | 778 | } |
| 779 | 779 | const value = this.map.get(key); |
| 780 | 780 | const valueSize = LimitedMapMemoryLimitedMap.estimateStringSize(value); |
| 781 | 781 | this.map.delete(key); |
| 782 | 782 | this.currentMemory -= valueSize; |
| 783 | 783 | |
| @@ -842,7 +842,7 @@ export class LimitedMap { | ||
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | /** |
| 845 | 845 | * Makes the LimitedMapMemoryLimitedMap iterable. |
| 846 | 846 | * @returns {Iterator} - Iterator over [key, value] pairs. |
| 847 | 847 | */ |
| 848 | 848 | [Symbol.iterator]() { |