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.
This commit is contained in:
David Snelling 2026-03-22 16:52:02 -07:00
parent 3dd5ac0d8c
commit 54865b3505

View file

@ -64,7 +64,7 @@ export class UnifiedCache {
private typeAccessCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 } private typeAccessCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
private totalAccessCount = 0 private totalAccessCount = 0
private currentSize = 0 private currentSize = 0
private readonly maxSize: number private maxSize: number
private readonly config: UnifiedCacheConfig private readonly config: UnifiedCacheConfig
// Memory management // Memory management
@ -433,6 +433,28 @@ export class UnifiedCache {
return deleted 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 * Clear cache or specific type
*/ */