aboutsummaryrefslogtreecommitdiff
path: root/internal/lru/weight.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/lru/weight.go')
-rw-r--r--internal/lru/weight.go29
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()
+}