v2

package
v1.4.8 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMustProvidePositiveSize = errors.New("must provide a positive size")

Functions

This section is empty.

Types

type ARCCache

type ARCCache struct {
	// contains filtered or unexported fields
}

ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). ARC is an enhancement over the standard LRU cache in that tracks both frequency and recency of use. This avoids a burst in access to new entries from evicting the frequently used older entries. It adds some additional tracking overhead to a standard LRU cache, computationally it is roughly 2x the cost, and the extra memory overhead is linear with the size of the cache. ARC has been patented by IBM, but is similar to the TwoQueueCache (2Q) which requires setting parameters.

func NewARC

func NewARC(size int) (*ARCCache, error)

NewARC creates an ARC of the given size

func (*ARCCache) Add

func (x *ARCCache) Add(key string, value interface{})

Add adds a value to the cache.

func (*ARCCache) Cap

func (x *ARCCache) Cap() int

Cap returns the capacity of the cache

func (*ARCCache) Contains

func (x *ARCCache) Contains(key string) bool

Contains is used to check if the cache contains a key without updating recency or frequency.

func (*ARCCache) Get

func (x *ARCCache) Get(key string) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*ARCCache) Keys

func (x *ARCCache) Keys() []string

Keys returns all the cached keys

func (*ARCCache) Len

func (x *ARCCache) Len() int

Len returns the number of cached entries

func (*ARCCache) Peek

func (x *ARCCache) Peek(key string) (value interface{}, ok bool)

Peek is used to inspect the cache value of a key without updating recency or frequency.

func (*ARCCache) Purge

func (x *ARCCache) Purge()

Purge is used to clear the cache

func (*ARCCache) Remove

func (x *ARCCache) Remove(key string)

Remove is used to purge a key from the cache

func (*ARCCache) Values

func (x *ARCCache) Values() []interface{}

Values returns all the cached values

type EvictCallback

type EvictCallback[K comparable, V any] func(key K, value V)

EvictCallback is used to get a callback when a cache entry is evicted

type LRU

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

LRU implements a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*LRU[K, V], error)

NewLRU constructs an LRU of the given size

func (*LRU[K, V]) Add

func (c *LRU[K, V]) Add(key K, value V) (evicted bool)

Add adds a value to the cache. Returns true if an eviction occurred.

func (*LRU[K, V]) Cap

func (c *LRU[K, V]) Cap() int

Cap returns the capacity of the cache

func (*LRU[K, V]) Contains

func (c *LRU[K, V]) Contains(key K) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*LRU[K, V]) Get

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

Get looks up a key's value from the cache.

func (*LRU[K, V]) GetOldest

func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool)

GetOldest returns the oldest entry

func (*LRU[K, V]) Keys

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

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*LRU[K, V]) Len

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

Len returns the number of items in the cache.

func (*LRU[K, V]) Peek

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

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Purge

func (c *LRU[K, V]) Purge()

Purge is used to completely clear the cache.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU[K, V]) RemoveOldest

func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU[K, V]) Resize

func (c *LRU[K, V]) Resize(size int) (evicted int)

Resize changes the cache size.

func (*LRU[K, V]) Values

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

Values returns a slice of the values in the cache, from oldest to newest.

type LRUCache

type LRUCache[K comparable, V any] interface {
	// Adds a value to the cache, returns true if an eviction occurred and
	// updates the "recently used"-ness of the key.
	Add(key K, value V) bool

	// Returns key's value from the cache and
	// updates the "recently used"-ness of the key. #value, isFound
	Get(key K) (value V, ok bool)

	// Checks if a key exists in cache without updating the recent-ness.
	Contains(key K) (ok bool)

	// Returns key's value without updating the "recently used"-ness of the key.
	Peek(key K) (value V, ok bool)

	// Removes a key from the cache.
	Remove(key K) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (K, V, bool)

	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (K, V, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []K

	// Values returns a slice of the values in the cache, from oldest to newest.
	Values() []V

	// Returns the number of items in the cache.
	Len() int

	// Returns the capacity of the cache.
	Cap() int

	// Clears all cache entries.
	Purge()

	// Resizes cache, returning number evicted
	Resize(int) int
}

LRUCache is the interface for simple LRU cache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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