lru

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: AGPL-3.0 Imports: 1 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 Cache

type Cache[Key comparable, Value 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 an LRU cache. It is not safe for concurrent access.

func New

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

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

func (*Cache[Key, Value]) Add

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

Add adds a value to the cache.

func (*Cache[Key, Value]) Clear

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

Clear purges all stored items from the cache.

func (*Cache[Key, Value]) Get

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

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

func (*Cache[Key, Value]) Len

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

Len returns the number of items in the cache.

func (*Cache[Key, Value]) Remove

func (c *Cache[Key, Value]) Remove(key Key)

Remove removes the provided key from the cache.

func (*Cache[Key, Value]) RemoveOldest

func (c *Cache[Key, Value]) RemoveOldest()

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