Documentation
¶
Index ¶
- Constants
- type LFUCache
- func NewLFUCache[K comparable, V any](capacity int) *LFUCache[K, V]
- func NewLFUCacheWithEvictionCallback[K comparable, V any](capacity int, onEviction base.EvictionCallback[K, V]) *LFUCache[K, V]
- func NewLFUCacheWithEvictionSize[K comparable, V any](capacity int, evictionSize int) *LFUCache[K, V]
- func NewLFUCacheWithEvictionSizeAndCallback[K comparable, V any](capacity int, evictionSize int, onEviction base.EvictionCallback[K, V]) *LFUCache[K, V]
- func (c *LFUCache[K, V]) Algorithm() string
- func (c *LFUCache[K, V]) All() map[K]V
- func (c *LFUCache[K, V]) Capacity() int
- func (c *LFUCache[K, V]) Delete(key K) bool
- func (c *LFUCache[K, V]) DeleteLeastFrequent() (k K, v V, ok bool)
- func (c *LFUCache[K, V]) DeleteMany(keys []K) map[K]bool
- func (c *LFUCache[K, V]) Get(key K) (value V, ok bool)
- func (c *LFUCache[K, V]) GetMany(keys []K) (map[K]V, []K)
- func (c *LFUCache[K, V]) Has(key K) bool
- func (c *LFUCache[K, V]) HasMany(keys []K) map[K]bool
- func (c *LFUCache[K, V]) Keys() []K
- func (c *LFUCache[K, V]) Len() int
- func (c *LFUCache[K, V]) Peek(key K) (value V, ok bool)
- func (c *LFUCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
- func (c *LFUCache[K, V]) Purge()
- func (c *LFUCache[K, V]) Range(f func(K, V) bool)
- func (c *LFUCache[K, V]) Set(key K, value V)
- func (c *LFUCache[K, V]) SetMany(items map[K]V)
- func (c *LFUCache[K, V]) SizeBytes() int64
- func (c *LFUCache[K, V]) Values() []V
Constants ¶
const ( // DefaultEvictionSize is the number of elements to evict when the cache is full. // This provides a balance between memory efficiency and performance. DefaultEvictionSize = 1 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LFUCache ¶
type LFUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LFUCache is a Least Frequently Used cache implementation using O(1) frequency tracking. Items are evicted by lowest access frequency, with LRU tiebreaking within the same frequency. It is not safe for concurrent access and should be wrapped with a thread-safe layer if needed.
func NewLFUCache ¶
func NewLFUCache[K comparable, V any](capacity int) *LFUCache[K, V]
NewLFUCache creates a new LFU cache with the specified capacity. Uses the default eviction size of 1 element when the cache is full.
func NewLFUCacheWithEvictionCallback ¶ added in v0.2.0
func NewLFUCacheWithEvictionCallback[K comparable, V any](capacity int, onEviction base.EvictionCallback[K, V]) *LFUCache[K, V]
NewLFUCacheWithEvictionCallback creates a new LFU cache with the specified capacity and eviction callback. The callback will be called whenever an item is evicted from the cache. Uses the default eviction size of 1 element when the cache is full.
func NewLFUCacheWithEvictionSize ¶
func NewLFUCacheWithEvictionSize[K comparable, V any](capacity int, evictionSize int) *LFUCache[K, V]
NewLFUCacheWithEvictionSize creates a new LFU cache with the specified capacity and eviction size. The eviction size determines how many elements are removed when the cache reaches capacity.
func NewLFUCacheWithEvictionSizeAndCallback ¶ added in v0.2.0
func NewLFUCacheWithEvictionSizeAndCallback[K comparable, V any](capacity int, evictionSize int, onEviction base.EvictionCallback[K, V]) *LFUCache[K, V]
NewLFUCacheWithEvictionSizeAndCallback creates a new LFU cache with the specified capacity, eviction size, and eviction callback. This is the main constructor for LFU caches. The cache will evict the least frequently used items when it reaches capacity.
func (*LFUCache[K, V]) Algorithm ¶
Algorithm returns the name of the eviction algorithm used by the cache.
func (*LFUCache[K, V]) All ¶ added in v0.9.0
func (c *LFUCache[K, V]) All() map[K]V
All returns all key-value pairs in the cache.
func (*LFUCache[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).
func (*LFUCache[K, V]) DeleteLeastFrequent ¶
DeleteLeastFrequent removes and returns the least frequently used item from the cache. Among items with the same frequency, the least recently used is evicted. Returns the key, value, and a boolean indicating if an item was removed. Time complexity: O(1).
func (*LFUCache[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 (*LFUCache[K, V]) Get ¶
Get retrieves a value from the cache and increments its frequency count. Returns the value and a boolean indicating if the key was found. Time complexity: O(1).
func (*LFUCache[K, V]) GetMany ¶
func (c *LFUCache[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. Each found key has its frequency count incremented.
func (*LFUCache[K, V]) Has ¶
Has checks if a key exists in the cache. This operation does not affect the frequency count of the key.
func (*LFUCache[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. This operation does not affect the frequency count of any keys.
func (*LFUCache[K, V]) Keys ¶
func (c *LFUCache[K, V]) Keys() []K
Keys returns all keys currently in the cache.
func (*LFUCache[K, V]) Len ¶
Len returns the current number of items in the cache. Time complexity: O(1).
func (*LFUCache[K, V]) Peek ¶
Peek retrieves a value from the cache without updating the frequency count. Returns the value and a boolean indicating if the key was found. This operation does not affect the eviction order.
func (*LFUCache[K, V]) PeekMany ¶
func (c *LFUCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
PeekMany retrieves multiple values from the cache without updating frequency counts. Returns a map of found key-value pairs and a slice of missing keys. This operation does not affect the eviction order.
func (*LFUCache[K, V]) Purge ¶
func (c *LFUCache[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 (*LFUCache[K, V]) Range ¶
Range iterates over all key-value pairs in the cache. The iteration stops if the function returns false.
func (*LFUCache[K, V]) Set ¶
func (c *LFUCache[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 its frequency is incremented. If the cache is at capacity, the least frequently used items are evicted. Time complexity: O(1) average case, O(evictionSize) worst case when eviction occurs.
func (*LFUCache[K, V]) SetMany ¶
func (c *LFUCache[K, V]) SetMany(items map[K]V)
SetMany stores multiple key-value pairs in the cache. Each key-value pair is processed individually, so frequency counts are updated correctly.