lru

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package lru implements an LRU cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

type LRU[Key comparable, Value any] struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key Key, value Value)
	// contains filtered or unexported fields
}

LRU is an LRU cache. It is not safe for concurrent access.

func New

func New[Key comparable, Value any](maxEntries int) *LRU[Key, Value]

New creates a new LRU. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func NewWithOnEvicted added in v1.0.1

func NewWithOnEvicted[Key comparable, Value any](maxEntries int, onEvicted func(Key, Value)) *LRU[Key, Value]

New creates a new LRU with OnEvicted. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*LRU[Key, Value]) Add

func (c *LRU[Key, Value]) Add(key Key, value Value)

Add adds a value to the cache.

func (*LRU[Key, Value]) ApplyRO

func (c *LRU[Key, Value]) ApplyRO(f func(generic_lru.Cache[Key, Value]))

func (*LRU[Key, Value]) ApplyRW

func (c *LRU[Key, Value]) ApplyRW(f func(generic_lru.Cache[Key, Value]))

func (*LRU[Key, Value]) Clear

func (c *LRU[Key, Value]) Clear()

Clear purges all stored items from the cache.

func (*LRU[Key, Value]) Container added in v1.0.2

func (c *LRU[Key, Value]) Container() (interface{}, error)

Container returns the underlying container.

func (*LRU[Key, Value]) Get

func (c *LRU[Key, Value]) Get(key Key) (value Value, ok bool)

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

func (*LRU[Key, Value]) GetOldest

func (c *LRU[Key, Value]) GetOldest() (key Key, value Value, ok bool)

RemoveOldest gets the oldest item from the cache.

func (*LRU[Key, Value]) Len

func (c *LRU[Key, Value]) Len() int

Len returns the number of items in the cache.

func (*LRU[Key, Value]) Remove

func (c *LRU[Key, Value]) Remove(key Key) (value Value, ok bool)

Remove removes the provided key from the cache.

func (*LRU[Key, Value]) RemoveOldest

func (c *LRU[Key, Value]) RemoveOldest() (key Key, value Value, ok bool)

RemoveOldest removes the oldest item from the cache.

Jump to

Keyboard shortcuts

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