Documentation
¶
Index ¶
- type ARCCache
- func (c *ARCCache[K, V]) Algorithm() string
- func (c *ARCCache[K, V]) Capacity() int
- func (c *ARCCache[K, V]) Delete(key K) bool
- func (c *ARCCache[K, V]) DeleteMany(keys []K) map[K]bool
- func (c *ARCCache[K, V]) DeleteOldest() (k K, v V, ok bool)
- func (c *ARCCache[K, V]) Get(key K) (value V, ok bool)
- func (c *ARCCache[K, V]) GetMany(keys []K) (map[K]V, []K)
- func (c *ARCCache[K, V]) Has(key K) bool
- func (c *ARCCache[K, V]) HasMany(keys []K) map[K]bool
- func (c *ARCCache[K, V]) Keys() []K
- func (c *ARCCache[K, V]) Len() int
- func (c *ARCCache[K, V]) Peek(key K) (value V, ok bool)
- func (c *ARCCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
- func (c *ARCCache[K, V]) Purge()
- func (c *ARCCache[K, V]) Range(f func(K, V) bool)
- func (c *ARCCache[K, V]) Set(key K, value V)
- func (c *ARCCache[K, V]) SetMany(items map[K]V)
- func (c *ARCCache[K, V]) SizeBytes() int64
- func (c *ARCCache[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ARCCache ¶
type ARCCache[K comparable, V any] struct { // contains filtered or unexported fields }
ARCCache is an Adaptive Replacement Cache implementation. It automatically balances between LRU and LFU policies based on access patterns. The cache maintains four lists: - T1: Recently accessed items (LRU) - T2: Frequently accessed items (LRU) - B1: Ghost entries for T1 (recently evicted from T1) - B2: Ghost entries for T2 (recently evicted from T2)
The adaptive parameter 'p' determines the balance between T1 and T2 sizes. When p=0, the cache behaves like pure LRU. When p=capacity, the cache behaves like pure LFU.
It is not safe for concurrent access and should be wrapped with a thread-safe layer if needed.
func NewARCCache ¶
func NewARCCache[K comparable, V any](capacity int) *ARCCache[K, V]
NewARCCache creates a new ARC cache with the specified capacity. The cache will adaptively balance between LRU and LFU eviction policies.
func NewARCCacheWithEvictionCallback ¶
func NewARCCacheWithEvictionCallback[K comparable, V any](capacity int, onEviction base.EvictionCallback[K, V]) *ARCCache[K, V]
NewARCCacheWithEvictionCallback creates a new ARC cache with the specified capacity and eviction callback. The callback will be called whenever an item is evicted from the cache.
func (*ARCCache[K, V]) Algorithm ¶
Algorithm returns the name of the eviction algorithm used by the cache.
func (*ARCCache[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 (*ARCCache[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 (*ARCCache[K, V]) DeleteOldest ¶
DeleteOldest removes and returns the least recently used item from the cache. For ARC, this means removing from the end of T1 or T2 based on the current state. Returns the key, value, and a boolean indicating if an item was found.
func (*ARCCache[K, V]) Get ¶
Get retrieves a value from the cache and updates the access order. Returns the value and a boolean indicating if the key was found.
func (*ARCCache[K, V]) GetMany ¶
func (c *ARCCache[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 (*ARCCache[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 (*ARCCache[K, V]) Keys ¶
func (c *ARCCache[K, V]) Keys() []K
Keys returns all keys currently in the cache.
func (*ARCCache[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 (*ARCCache[K, V]) PeekMany ¶
func (c *ARCCache[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 (*ARCCache[K, V]) Purge ¶
func (c *ARCCache[K, V]) Purge()
Purge removes all keys and values from the cache.
func (*ARCCache[K, V]) Range ¶
Range iterates over all key-value pairs in the cache. The iteration stops if the function returns false.
func (*ARCCache[K, V]) Set ¶
func (c *ARCCache[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, items are evicted according to the ARC algorithm. Time complexity: O(1) average case.
func (*ARCCache[K, V]) SetMany ¶
func (c *ARCCache[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.