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/new.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 internal/lru/new.go (limited to 'internal/lru/new.go') diff --git a/internal/lru/new.go b/internal/lru/new.go new file mode 100644 index 00000000..f683416a --- /dev/null +++ b/internal/lru/new.go @@ -0,0 +1,23 @@ +package lru + +import "container/list" + +// New creates a cache with a maximum total weight. +// +// New panics if maxWeight is negative or weightFn is nil. +func New[K comparable, V any](maxWeight int64, weightFn WeightFunc[K, V], onEvict OnEvictFunc[K, V]) *Cache[K, V] { + if maxWeight < 0 { + panic("lru: negative max weight") + } + + if weightFn == nil { + panic("lru: nil weight function") + } + + return &Cache[K, V]{ + maxWeight: maxWeight, + weightFn: weightFn, + onEvict: onEvict, + items: make(map[K]*list.Element), + } +} -- cgit v1.3.1-10-gc9f91