tinylfu

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TinyLFUCache

type TinyLFUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TinyLFUCache 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 NewTinyLFUCache

func NewTinyLFUCache[K comparable, V any](capacity int) *TinyLFUCache[K, V]

NewTinyLFUCache creates a new TinyLFU cache with the specified capacity. The cache will evict the least recently used items when it reaches capacity.

func NewTinyLFUCacheWithEvictionCallback

func NewTinyLFUCacheWithEvictionCallback[K comparable, V any](capacity int, onEviction base.EvictionCallback[K, V]) *TinyLFUCache[K, V]

NewTinyLFUCacheWithEvictionCallback creates a new TinyLFU cache with the specified capacity and eviction callback. The callback will be called whenever an item is evicted from the cache.

func (*TinyLFUCache[K, V]) Algorithm

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) All

func (c *TinyLFUCache[K, V]) All() map[K]V

All returns all key-value pairs in the cache.

func (*TinyLFUCache[K, V]) Capacity

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

Capacity returns the maximum number of items the cache can hold. Returns 0 if the cache has unlimited capacity.

func (*TinyLFUCache[K, V]) Delete

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) DeleteMany

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) Get

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

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

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) Has

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

Has checks if a key exists in the cache.

func (*TinyLFUCache[K, V]) HasMany

func (c *TinyLFUCache[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.

func (*TinyLFUCache[K, V]) Keys

func (c *TinyLFUCache[K, V]) Keys() []K

Keys returns all keys currently in the cache.

func (*TinyLFUCache[K, V]) Len

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

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

func (*TinyLFUCache[K, V]) Peek

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

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

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) Purge

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) Range

func (c *TinyLFUCache[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.

func (*TinyLFUCache[K, V]) Set

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) SetMany

func (c *TinyLFUCache[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.

func (*TinyLFUCache[K, V]) SizeBytes

func (c *TinyLFUCache[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 (*TinyLFUCache[K, V]) Values

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

Values returns all values currently in the cache.

Jump to

Keyboard shortcuts

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