lru

package
v5.0.0-...-2e8bb31 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: ISC Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T comparable] struct {
	// contains filtered or unexported fields
}

Cache implements a least-recently-updated (LRU) cache with nearly O(1) lookups and inserts. Items are added to the cache up to a limit, at which point further additions will evict the least recently added item. The zero value is not valid and Caches must be created with NewCache. All Cache methods are concurrent safe.

func NewCache

func NewCache[T comparable](limit int) Cache[T]

NewCache creates an initialized and empty LRU cache.

func (*Cache[T]) Add

func (c *Cache[T]) Add(item T)

Add adds an item to the LRU cache, removing the oldest item if the new item is not already a member, or marking item as the most recently added item if it is already present.

func (*Cache[T]) Contains

func (c *Cache[T]) Contains(v T) bool

Contains checks whether v is a member of the LRU cache.

type Map

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

Map implements a least-recently-updated (LRU) map with nearly O(1) lookups and inserts. Items are added to the cache up to a limit, at which point further additions will evict the least recently added item. The zero value is not valid and Maps must be created with NewMap. All Map methods are concurrent safe.

func NewMap

func NewMap[K comparable, V any](limit int) Map[K, V]

NewMap creates an initialized and empty LRU map.

func (*Map[K, V]) Add

func (m *Map[K, V]) Add(key K, value V)

Add adds an item to the LRU map, removing the oldest item if the new item exceeds the total map capacity and is not already a member, or marking the already-present item as the most recently added item.

func (*Map[K, V]) Contains

func (m *Map[K, V]) Contains(key K) bool

Contains checks whether key is a member of the LRU map. It does modify the priority of any items.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Get fetches the value from the LRU map, prioritizing it to evict it last after all other items. The second return value is true if the value was present, and false otherwise.

func (*Map[K, V]) Hit

func (m *Map[K, V]) Hit(key K) bool

Hit marks the value under a key as recently used and prioritizes it to evict it last after all other items. Returns true if the value was present, and false otherwise.

Hit is equivalent to Get but discards the value.

Jump to

Keyboard shortcuts

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