Documentation
¶
Index ¶
- type LRUCache
- func (c *LRUCache[K, V]) Algorithm() string
- func (c *LRUCache[K, V]) All() map[K]V
- func (c *LRUCache[K, V]) Capacity() int
- func (c *LRUCache[K, V]) Delete(key K) bool
- func (c *LRUCache[K, V]) DeleteMany(keys []K) map[K]bool
- func (c *LRUCache[K, V]) DeleteOldest() (k K, v V, ok bool)
- func (c *LRUCache[K, V]) Get(key K) (value V, ok bool)
- func (c *LRUCache[K, V]) GetMany(keys []K) (map[K]V, []K)
- func (c *LRUCache[K, V]) Has(key K) bool
- func (c *LRUCache[K, V]) HasMany(keys []K) map[K]bool
- func (c *LRUCache[K, V]) Keys() []K
- func (c *LRUCache[K, V]) Len() int
- func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool)
- func (c *LRUCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
- func (c *LRUCache[K, V]) Purge()
- func (c *LRUCache[K, V]) Range(f func(K, V) bool)
- func (c *LRUCache[K, V]) Set(key K, value V)
- func (c *LRUCache[K, V]) SetMany(items map[K]V)
- func (c *LRUCache[K, V]) SizeBytes() int64
- func (c *LRUCache[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRUCache ¶
type LRUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LRUCache is a Least Recently Used cache implementation. It is not safe for concurrent access and should be wrapped with a thread-safe layer if needed.
func NewLRUCache ¶
func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]
NewLRUCache creates a new LRU cache with the specified capacity. The cache will evict the least recently used items when it reaches capacity.
func NewLRUCacheWithEvictionCallback ¶ added in v0.2.0
func NewLRUCacheWithEvictionCallback[K comparable, V any](capacity int, onEviction base.EvictionCallback[K, V]) *LRUCache[K, V]
NewLRUCacheWithEvictionCallback creates a new LRU cache with the specified capacity and eviction callback. The callback will be called whenever an item is evicted from the cache.
func (*LRUCache[K, V]) Algorithm ¶
Algorithm returns the name of the eviction algorithm used by the cache. This is used for debugging and monitoring purposes.
func (*LRUCache[K, V]) All ¶ added in v0.9.0
func (c *LRUCache[K, V]) All() map[K]V
All returns all key-value pairs in the cache.
func (*LRUCache[K, V]) Capacity ¶
Capacity returns the maximum number of items the cache can hold. Returns 0 if the cache has unlimited capacity.
func (*LRUCache[K, V]) Delete ¶
Delete removes a key from the cache. Returns true if the key was found and removed, false otherwise. Time complexity: O(1) average case.
func (*LRUCache[K, V]) DeleteMany ¶
DeleteMany removes multiple keys from the cache. Returns a map where keys are the input keys and values indicate if the key was found and removed.
func (*LRUCache[K, V]) DeleteOldest ¶
DeleteOldest removes and returns the least recently used item from the cache. Returns the key, value, and a boolean indicating if an item was removed. This method is used internally for eviction when the cache reaches capacity. Time complexity: O(1) - removes from the back of the list.
func (*LRUCache[K, V]) Get ¶
Get retrieves a value from the cache and makes it the most recently used item. Returns the value and a boolean indicating if the key was found.
func (*LRUCache[K, V]) GetMany ¶
func (c *LRUCache[K, V]) GetMany(keys []K) (map[K]V, []K)
GetMany retrieves multiple values from the cache. Returns a map of found key-value pairs and a slice of missing keys.
func (*LRUCache[K, V]) HasMany ¶
HasMany checks if multiple keys exist in the cache. Returns a map where keys are the input keys and values indicate existence.
func (*LRUCache[K, V]) Keys ¶
func (c *LRUCache[K, V]) Keys() []K
Keys returns all keys currently in the cache.
func (*LRUCache[K, V]) Len ¶
Len returns the current number of items in the cache. Time complexity: O(1) - the list maintains its length.
func (*LRUCache[K, V]) Peek ¶
Peek retrieves a value from the cache without updating the access order. Returns the value and a boolean indicating if the key was found.
func (*LRUCache[K, V]) PeekMany ¶
func (c *LRUCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
PeekMany retrieves multiple values from the cache without updating access order. Returns a map of found key-value pairs and a slice of missing keys.
func (*LRUCache[K, V]) Purge ¶
func (c *LRUCache[K, V]) Purge()
Purge removes all keys and values from the cache. This operation resets the cache to its initial state. Time complexity: O(1) - just reallocates the data structures.
func (*LRUCache[K, V]) Range ¶
Range iterates over all key-value pairs in the cache. The iteration stops if the function returns false.
func (*LRUCache[K, V]) Set ¶
func (c *LRUCache[K, V]) Set(key K, value V)
Set stores a key-value pair in the cache. If the key already exists, its value is updated and it becomes the most recently used item. If the cache is at capacity, the least recently used item is evicted. Time complexity: O(1) average case, O(n) worst case when eviction occurs.
func (*LRUCache[K, V]) SetMany ¶
func (c *LRUCache[K, V]) SetMany(items map[K]V)
SetMany stores multiple key-value pairs in the cache. This is more efficient than calling Set multiple times.