lru

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 3 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[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is an LRU cache that enforces a maximum weight capacity and age limit for its elements. The LRU cache performs locking internally and is thread-safe. It also keeps track of the hit/miss statistics.

func NewCache

func NewCache[K comparable, V any]() *Cache[K, V]

NewCache creates a new LRU cache with a weight capacity of 16384 and a maximum age of 1hr.

func (*Cache[K, V]) Clear

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

Clear empties the cache but does not reset the hit/miss statistics.

func (*Cache[K, V]) Delete

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

Delete removes an element from the cache by key.

func (*Cache[K, V]) DeletePredicate

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

Delete removes elements from the cache whose keys match the predicate function.

func (*Cache[K, V]) Exists

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

Exists indicates if the key is in the cache.

func (*Cache[K, V]) Hits

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

Hits returns the total number of cache hits. This number can technically overflow.

func (*Cache[K, V]) Len

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

Len returns the number of elements in the cache.

func (*Cache[K, V]) Load

func (c *Cache[K, V]) Load(key K, options ...Option) (value V, ok bool)

Load looks up an element in the cache.

func (*Cache[K, V]) LoadOrStore

func (c *Cache[K, V]) LoadOrStore(key K, newValue V, options ...Option) (value V, found bool)

LoadOrStore looks up an element in the cache. If the element is not found, the new value is stored and returned instead.

func (*Cache[K, V]) MaxAge

func (c *Cache[K, V]) MaxAge() time.Duration

MaxAge returns the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.

func (*Cache[K, V]) MaxWeight

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

MaxWeight returns the weight limit set for this cache.

func (*Cache[K, V]) Misses

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

Misses returns the total number of cache misses. This number can technically overflow.

func (*Cache[K, V]) SetMaxAge

func (c *Cache[K, V]) SetMaxAge(ttl time.Duration) error

SetMaxAge sets the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.

func (*Cache[K, V]) SetMaxWeight

func (c *Cache[K, V]) SetMaxWeight(weight int) error

SetMaxAge sets the total weight limit of elements in this cache. If not specified, elements default to a weight of 1.

func (*Cache[K, V]) Store

func (c *Cache[K, V]) Store(key K, value V, options ...Option)

Store inserts an element to the front of the cache. The weight must be 1 or greater and cannot exceed the cache's maximum weight limit.

func (*Cache[K, V]) ToMap

func (c *Cache[K, V]) ToMap() map[K]V

ToMap returns the elements currently in the cache in a newly allocated map.

func (*Cache[K, V]) Weight

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

Weight returns the total weight of all the elements in the cache.

type Option

type Option func(opts *cacheOptions)

Option is used to customize cache operations.

func Bump

func Bump(bump bool) Option

Bump indicates whether or not a loaded element is bumped to the head of the cache. Bumping is the default behavior. This option is applicable to load operations.

func MaxAge

func MaxAge(maxAge time.Duration) Option

MaxAge indicates to ignore elements that have not been inserted or bumped recently. This option is applicable to load operations.

func NoBump

func NoBump() Option

NoBump indicates not to bump a loaded element to the head of the cache. This option is applicable to load operations.

func Weight

func Weight(weight int) Option

Weight sets the weight of the element stored in the cache. It must be 1 or greater and cannot exceed the cache's maximum weight limit. The default weight is 1. Elements are evicted when the total weight of all elements exceeds the cache's capacity. This option is applicable to store operations.

Jump to

Keyboard shortcuts

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