lru

package
v1.2.117 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 EvictCallbackFunc added in v1.2.54

type EvictCallbackFunc[K comparable, V any] interface {
	Evict(key K, value V)
}

type LRU

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

LRU is like a Go map[K]V but implements a non-thread safe fixed size LRU cache. Loads, stores, and deletes run in amortized constant time.

func New

func New[K comparable, V any](size int) *LRU[K, V]

New 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, with updating the "recently used"-ness of the key. Returns true if an eviction occurred.

func (*LRU[K, V]) Cap added in v1.2.54

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

Cap returns the capacity of the cache.

func (*LRU[K, V]) CompareAndDelete added in v1.2.116

func (c *LRU[K, V]) CompareAndDelete(key K, old V) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

func (*LRU[K, V]) CompareAndSwap added in v1.2.116

func (c *LRU[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.

func (*LRU[K, V]) Contains

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

Contains reports whether key is within the cache. The ok result indicates whether value was found in the cache. Without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Delete added in v1.2.116

func (c *LRU[K, V]) Delete(key K)

Delete deletes the value for a key.

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, with updating the "recently used"-ness of the key.

func (*LRU[K, V]) GetOldest

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

GetOldest returns the oldest entry, without updating the "recently used"-ness or deleting it for being stale. Without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Init

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

Init initializes or clears LRU l.

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. Without updating the "recently used"-ness of the key.

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]) Load added in v1.2.116

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

Load returns the value stored in the cache for a key, or zero if no value is present. The ok result indicates whether value was found in the cache.

func (*LRU[K, V]) LoadAndDelete added in v1.2.116

func (c *LRU[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*LRU[K, V]) LoadOrStore added in v1.2.116

func (c *LRU[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*LRU[K, V]) Peek

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

Peek returns the value stored in the cache for a key, or zero if no value is present. The ok result indicates whether value was found in the cache. Without updating the "recently used"-ness of the key.

func (*LRU[K, V]) PeekAndDeleteOldest added in v1.2.116

func (c *LRU[K, V]) PeekAndDeleteOldest() (key K, value V, loaded bool)

PeekAndDeleteOldest deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*LRU[K, V]) PeekOldest added in v1.2.116

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

PeekOldest returns the value stored in the cache for the oldest entry, or zero if no value is present. The ok result indicates whether value was found in the cache. 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]) Range added in v1.2.116

func (c *LRU[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the lru from oldest to newest. If f returns false, range stops the iteration. Without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Remove

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

Remove removes the provided key from the cache, returning true 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]) SetEvictCallback

func (c *LRU[K, V]) SetEvictCallback(onEvict EvictCallback[K, V]) *LRU[K, V]

SetEvictCallback sets a callback when a cache entry is evicted

func (*LRU[K, V]) SetEvictCallbackFunc added in v1.2.54

func (c *LRU[K, V]) SetEvictCallbackFunc(onEvict func(key K, value V)) *LRU[K, V]

SetEvictCallbackFunc sets a callback func when a cache entry is evicted

Deprecated, use SetEvictCallback instead.

func (*LRU[K, V]) Store added in v1.2.116

func (c *LRU[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*LRU[K, V]) Swap added in v1.2.116

func (c *LRU[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

Jump to

Keyboard shortcuts

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