Documentation
¶
Index ¶
- type Cache
- type EvictCallback
- type LRU
- func (c *LRU[K, V]) Add(key K, value V) (evicted bool)
- func (c *LRU[K, V]) Cap() int
- func (c *LRU[K, V]) Get(key K) (V, bool)
- func (c *LRU[K, V]) GetOrCompute(key K, compute func() V) V
- func (c *LRU[K, V]) HitRate() float64
- func (c *LRU[K, V]) Len() int
- func (c *LRU[K, V]) Purge()
- func (c *LRU[K, V]) Remove(key K) bool
- func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { Add(key K, value V) (evicted bool) Get(key K) (value V, ok bool) Purge() }
Cache defines a generic interface for a key-value storage with type constraints on keys and values. The keys must be of a type that supports comparison. Add inserts a new key-value pair, potentially evicting an item if necessary. Get retrieves a value associated with the given key, returning a boolean to indicate if the key was found. Purge clears all items from the cache.
type EvictCallback ¶
type EvictCallback[K comparable, V any] func(key K, value V)
EvictCallback is a function called when an entry is evicted. This includes evictions during Purge or Resize operations.
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU is a thread-safe, generic LRU cache with a fixed size. It has zero dependencies, high performance, and full features.
func NewLRU ¶
func NewLRU[K comparable, V any](size int) *LRU[K, V]
NewLRU creates a new LRU cache with the given size. Returns nil if size <= 0, acting as a disabled cache. Caps size at 100,000 for reasonableness.
func NewLRUEvict ¶
func NewLRUEvict[K comparable, V any](size int, onEvict EvictCallback[K, V]) *LRU[K, V]
NewLRUEvict creates a new LRU cache with an eviction callback. The callback is optional and called on evictions. Returns nil if size <= 0.
func (*LRU[K, V]) Add ¶
Add inserts or updates a key-value pair. Evicts the oldest if cache is full. Returns true if an eviction occurred.
func (*LRU[K, V]) Get ¶
Get retrieves a value by key if it exists. Returns the value and true if found, else zero and false. Updates the entry to most recently used.
func (*LRU[K, V]) GetOrCompute ¶
func (c *LRU[K, V]) GetOrCompute(key K, compute func() V) V
GetOrCompute retrieves a value or computes it if missing. Ensures no double computation under concurrency. Ideal for expensive computations like twwidth.
func (*LRU[K, V]) HitRate ¶
HitRate returns the cache hit ratio (0.0 to 1.0). Based on hits / (hits + misses).
func (*LRU[K, V]) Purge ¶
func (c *LRU[K, V]) Purge()
Purge clears all entries from the cache. Calls onEvict for each entry if set. Resets hit/miss counters.
func (*LRU[K, V]) Remove ¶
Remove deletes a key from the cache. Returns true if the key was found and removed.
func (*LRU[K, V]) RemoveOldest ¶
RemoveOldest removes and returns the least recently used item. Returns key, value, and true if an item was removed. Calls onEvict if set.