From 54865b3505b850fa89c0f01ee156ae5be652b7c5 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Sun, 22 Mar 2026 16:52:02 -0700 Subject: [PATCH] feat: add setMaxSize() for dynamic cache resizing UnifiedCache.setMaxSize() allows runtime resizing of the cache maximum. When the new limit is smaller than current usage, lowest-value items are evicted until the cache fits. Used by Cortex's ResourceManager to rebalance cache vs instance memory as brainy instances are created and evicted in multi-tenant deployments. Also adds getMaxSize() for observability. --- src/utils/unifiedCache.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/utils/unifiedCache.ts b/src/utils/unifiedCache.ts index dde561ac..1ee7f1e7 100644 --- a/src/utils/unifiedCache.ts +++ b/src/utils/unifiedCache.ts @@ -64,7 +64,7 @@ export class UnifiedCache { private typeAccessCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 } private totalAccessCount = 0 private currentSize = 0 - private readonly maxSize: number + private maxSize: number private readonly config: UnifiedCacheConfig // Memory management @@ -433,6 +433,28 @@ export class UnifiedCache { return deleted } + /** + * Dynamically resize the cache maximum. If the new size is smaller than + * the current usage, evicts lowest-value items until the cache fits within + * the new budget. Used by Cortex's ResourceManager to rebalance cache vs + * instance memory as brainy instances are created and evicted. + * + * @param newMaxSize - New maximum cache size in bytes + */ + setMaxSize(newMaxSize: number): void { + this.maxSize = Math.max(newMaxSize, 64 * 1024 * 1024) // Floor at 64MB + while (this.currentSize > this.maxSize && this.cache.size > 0) { + this.evictLowestValue() + } + } + + /** + * Get the current maximum cache size in bytes. + */ + getMaxSize(): number { + return this.maxSize + } + /** * Clear cache or specific type */