lru

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: Apache-2.0, MPL-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Default2QRecentRatio is the ratio of the 2Q cache dedicated
	// to recently added entries that have only been accessed once.
	Default2QRecentRatio = 0.25

	// Default2QGhostEntries is the default ratio of ghost
	// entries kept to track entries recently evicted
	Default2QGhostEntries = 0.50
)

Variables

This section is empty.

Functions

func New

func New[K comparable, V any](size int) (cache.Cache[K, V], error)

New creates an LRU of the given size.

func NewWithEvict

func NewWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (cache.Cache[K, V], error)

NewWithEvict constructs a fixed size cache with the given eviction callback.

Types

type Cache

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

Cache is a thread-safe fixed size LRU cache.

func (*Cache[K, V]) Add

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

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

func (*Cache[K, V]) Contains

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

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

func (*Cache[K, V]) ContainsOrAdd

func (c *Cache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) Get

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

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

func (*Cache[K, V]) Keys

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

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

func (*Cache[K, V]) Len

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

Len returns the number of items in the cache.

func (*Cache[K, V]) Peek

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

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

Purge is used to completely clear the cache.

func (*Cache[K, V]) Remove

func (c *Cache[K, V]) Remove(key K)

Remove removes the provided key from the cache.

func (*Cache[K, V]) RemoveOldest

func (c *Cache[K, V]) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

type TwoQueueCache

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

TwoQueueCache is a thread-safe fixed size 2Q cache. 2Q is an enhancement over the standard LRU cache in that it tracks both frequently and recently used entries separately. This avoids a burst in access to new entries from evicting frequently used entries. It adds some additional tracking overhead to the standard LRU cache, and is computationally about 2x the cost, and adds some metadata over head. The ARCCache is similar, but does not require setting any parameters.

func New2Q

func New2Q[K comparable, V any](size int) (*TwoQueueCache[K, V], error)

New2Q creates a new TwoQueueCache using the default values for the parameters.

func New2QParams

func New2QParams[K comparable, V any](size int, recentRatio float64, ghostRatio float64, onEvicted func(key K, value V)) (*TwoQueueCache[K, V], error)

New2QParams creates a new TwoQueueCache using the provided parameter values.

func New2QWithEvict

func New2QWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (*TwoQueueCache[K, V], error)

func (*TwoQueueCache[K, V]) Add

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

Add adds a value to the cache.

func (*TwoQueueCache[K, V]) Contains

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

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

func (*TwoQueueCache[K, V]) ContainsOrAdd

func (c *TwoQueueCache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*TwoQueueCache[K, V]) Get

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

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

func (*TwoQueueCache[K, V]) Keys

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

Keys returns a slice of the keys in the cache. The frequently used keys are first in the returned slice.

func (*TwoQueueCache[K, V]) Len

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

Len returns the number of items in the cache.

func (*TwoQueueCache[K, V]) Peek

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

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

func (*TwoQueueCache[K, V]) Purge

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

Purge is used to completely clear the cache.

func (*TwoQueueCache[K, V]) Remove

func (c *TwoQueueCache[K, V]) Remove(key K)

Remove removes the provided key from the cache.

func (*TwoQueueCache[K, V]) RemoveOldest

func (c *TwoQueueCache[K, V]) RemoveOldest()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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