arc

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

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

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

Algorithm returns the name of the eviction algorithm used by the cache.

func (*ARCCache[K, V]) Capacity

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

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

func (*ARCCache[K, V]) Delete

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

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

func (c *ARCCache[K, V]) DeleteOldest() (k K, v V, ok bool)

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

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

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

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

Has checks if a key exists in the cache.

func (*ARCCache[K, V]) HasMany

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

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

Keys returns all keys currently in the cache.

func (*ARCCache[K, V]) Len

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

Len returns the current number of items in the cache.

func (*ARCCache[K, V]) Peek

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

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

func (*ARCCache[K, V]) SizeBytes

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

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