lru

package
v0.0.0-...-9d29f1b Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package lru contains a typed Least-Recently-Used cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int
	// contains filtered or unexported fields
}

Cache is container type keyed by K, storing V, optionally evicting the least recently used items if a maximum size is exceeded.

The zero value is valid to use.

It is not safe for concurrent access.

The current implementation is just the traditional LRU linked list; a future implementation may be more advanced to avoid pathological cases.

func (*Cache[K, V]) Contains

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

Contains reports whether c contains key.

If found, key is moved to the front of the LRU.

func (*Cache[K, V]) Delete

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

Delete removes the provided key from the cache if it was present.

func (*Cache[K, V]) DeleteOldest

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

DeleteOldest removes the item from the cache that was least recently accessed. It is a no-op if the cache is empty.

func (*Cache[K, V]) Get

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

Get looks up a key's value from the cache, returning either the value or the zero value if it not present.

If found, key is moved to the front of the LRU.

func (*Cache[K, V]) GetOk

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

GetOk looks up a key's value from the cache, also reporting whether it was present.

If found, key is moved to the front of the LRU.

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

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

Set adds or replaces a value to the cache, set or updating its associated value.

If MaxEntries is non-zero and the length of the cache is greater after any addition, the least recently used value is evicted.

Jump to

Keyboard shortcuts

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