From fec8b64975f952e2fa5fa8478b09dced87f0fa7b Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 6 Mar 2026 04:43:03 +0800 Subject: internal/lru: Split --- internal/lru/weight.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 internal/lru/weight.go (limited to 'internal/lru/weight.go') 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() +} -- cgit v1.3.1-10-gc9f91