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:
parent
3dd5ac0d8c
commit
54865b3505
1 changed files with 23 additions and 1 deletions
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue