lfu

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
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. It is not safe for concurrent access and should be wrapped with a thread-safe layer if needed. Items are ordered by their access frequency, with least frequently used items at the front.

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

func (c *LFUCache[K, V]) Algorithm() string

Algorithm returns the name of the eviction algorithm used by the cache. This is used for debugging and monitoring purposes.

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]) Capacity

func (c *LFUCache[K, V]) Capacity() int

Capacity returns the maximum number of items the cache can hold.

func (*LFUCache[K, V]) Delete

func (c *LFUCache[K, V]) Delete(key K) bool

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 (*LFUCache[K, V]) DeleteLeastFrequent

func (c *LFUCache[K, V]) DeleteLeastFrequent() (k K, v V, ok bool)

DeleteLeastFrequent removes and returns the least frequently 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 front of the list.

func (*LFUCache[K, V]) DeleteMany

func (c *LFUCache[K, V]) DeleteMany(keys []K) map[K]bool

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

func (c *LFUCache[K, V]) Get(key K) (value V, ok bool)

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) average case.

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

func (c *LFUCache[K, V]) Has(key K) bool

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

func (c *LFUCache[K, V]) HasMany(keys []K) map[K]bool

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. The order of keys in the returned slice is not guaranteed to match frequency order.

func (*LFUCache[K, V]) Len

func (c *LFUCache[K, V]) Len() int

Len returns the current number of items in the cache. Time complexity: O(1) - the list maintains its length.

func (*LFUCache[K, V]) Peek

func (c *LFUCache[K, V]) Peek(key K) (value V, ok bool)

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

func (c *LFUCache[K, V]) Range(f func(K, V) bool)

Range iterates over all key-value pairs in the cache. The iteration stops if the function returns false. The iteration order is not guaranteed to match frequency order.

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. This is more efficient than calling Set multiple times. Each key-value pair is processed individually, so frequency counts are updated correctly.

func (*LFUCache[K, V]) SizeBytes added in v0.7.0

func (c *LFUCache[K, V]) SizeBytes() int64

SizeBytes returns the total size of all cache entries in bytes. For generic caches, this returns 0 as the size cannot be determined without type information. Specialized implementations should override this method.

func (*LFUCache[K, V]) Values

func (c *LFUCache[K, V]) Values() []V

Values returns all values currently in the cache. The order of values in the returned slice is not guaranteed to match frequency order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL