Documentation
¶
Overview ¶
Copyright 2025 atframework
Index ¶
- type LRUMap
- func (c *LRUMap[K, V]) Back() (K, V, bool)
- func (c *LRUMap[K, V]) Cap() int
- func (c *LRUMap[K, V]) Clear()
- func (c *LRUMap[K, V]) Contains(key K) bool
- func (c *LRUMap[K, V]) Delete(key K) bool
- func (c *LRUMap[K, V]) Front() (K, V, bool)
- func (c *LRUMap[K, V]) Get(key K, updateVisit bool) (V, bool)
- func (c *LRUMap[K, V]) Keys() []K
- func (c *LRUMap[K, V]) Len() int
- func (c *LRUMap[K, V]) PopBack() (K, V, bool)
- func (c *LRUMap[K, V]) PopFront() (K, V, bool)
- func (c *LRUMap[K, V]) Put(key K, value V)
- func (c *LRUMap[K, V]) Range(fn func(key K, value V) bool)
- func (c *LRUMap[K, V]) RangeReverse(fn func(key K, value V) bool)
- func (c *LRUMap[K, V]) Resize(newCapacity int)
- func (c *LRUMap[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRUMap ¶
type LRUMap[K comparable, V any] struct { // contains filtered or unexported fields }
LRUMap is a generic LRU (Least Recently Used) cache with map-like access. K must be comparable (used as map key), V can be any type.
func NewLRUMap ¶
func NewLRUMap[K comparable, V any](capacity int) *LRUMap[K, V]
NewLRUMap creates a new LRUMap with the specified capacity. If capacity <= 0, the cache will have unlimited capacity.
func (*LRUMap[K, V]) Back ¶
Back returns the least recently used key-value pair without removing it. Returns the zero values and false if the cache is empty.
func (*LRUMap[K, V]) Contains ¶
Contains checks if a key exists in the cache without updating its access time.
func (*LRUMap[K, V]) Delete ¶
Delete removes a key from the cache. Returns true if the key was found and removed, false otherwise.
func (*LRUMap[K, V]) Front ¶
Front returns the most recently used key-value pair without removing it. Returns the zero values and false if the cache is empty.
func (*LRUMap[K, V]) Get ¶
Get retrieves a value from the cache by key. If updateVisit is true, the item is moved to the front (most recently used). Returns the zero value and false if the key is not found.
func (*LRUMap[K, V]) Keys ¶
func (c *LRUMap[K, V]) Keys() []K
Keys returns all keys in the cache, ordered from most to least recently used.
func (*LRUMap[K, V]) PopBack ¶
PopBack removes and returns the least recently used key-value pair. Returns the zero values and false if the cache is empty.
func (*LRUMap[K, V]) PopFront ¶
PopFront removes and returns the most recently used key-value pair. Returns the zero values and false if the cache is empty.
func (*LRUMap[K, V]) Put ¶
func (c *LRUMap[K, V]) Put(key K, value V)
Put adds or updates a key-value pair in the cache. If the key already exists, its value is updated and moved to the front. If the cache is at capacity, the least recently used item is evicted.
func (*LRUMap[K, V]) Range ¶
Range iterates over all key-value pairs in the cache, from most to least recently used. If the callback returns false, iteration stops.
func (*LRUMap[K, V]) RangeReverse ¶
RangeReverse iterates over all key-value pairs in the cache, from least to most recently used. If the callback returns false, iteration stops.