diff options
| author | 2026-03-06 04:43:03 +0800 | |
|---|---|---|
| committer | 2026-03-06 04:43:03 +0800 | |
| commit | fec8b64975f952e2fa5fa8478b09dced87f0fa7b (patch) | |
| tree | 358cea12d4f20d6821202f040aeb9be58eb878a7 /internal/lru/weight.go | |
| parent | format/commitgraph/bloom: Add commit-graph bloom filters (diff) | |
| signature | No signature | |
internal/lru: Split
Diffstat (limited to 'internal/lru/weight.go')
| -rw-r--r-- | internal/lru/weight.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/lru/weight.go b/internal/lru/weight.go new file mode 100644 index 00000000..5ef552a1 --- /dev/null +++ b/internal/lru/weight.go @@ -0,0 +1,29 @@ +package lru + +// WeightFunc reports one entry's weight used for eviction budgeting. +// +// Returned weights MUST be non-negative. +type WeightFunc[K comparable, V any] func(key K, value V) int64 + +// Weight returns the current total weight. +func (cache *Cache[K, V]) Weight() int64 { + return cache.weight +} + +// MaxWeight returns the configured total weight budget. +func (cache *Cache[K, V]) MaxWeight() int64 { + return cache.maxWeight +} + +// SetMaxWeight updates the total weight budget and evicts LRU entries as +// needed. +// +// SetMaxWeight panics if maxWeight is negative. +func (cache *Cache[K, V]) SetMaxWeight(maxWeight int64) { + if maxWeight < 0 { + panic("lru: negative max weight") + } + + cache.maxWeight = maxWeight + cache.evictOverBudget() +} |
