lru

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: BSD-3-Clause Imports: 4 Imported by: 13

Documentation

Overview

Package lru provides the ONE standard LRU cache implementation using container package.

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 the standard LRU cache - ONE implementation, no duplicates

func NewCache

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

NewCache creates a new LRU cache - THE standard way

func NewCacheWithOnEvict

func NewCacheWithOnEvict[K comparable, V any](size int, onEvict func(K, V)) *Cache[K, V]

NewCacheWithOnEvict creates cache with eviction callback

func (*Cache[K, V]) Clear

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

Clear removes all items

func (*Cache[K, V]) Contains

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

Contains checks key existence

func (*Cache[K, V]) Delete

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

Delete removes value from cache

func (*Cache[K, V]) Evict

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

Evict removes a key from cache (required by Cacher interface)

func (*Cache[K, V]) Flush

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

Flush removes all entries from cache (required by Cacher interface)

func (*Cache[K, V]) Get

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

Get retrieves value from cache

func (*Cache[K, V]) Len

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

Len returns cache size

func (*Cache[K, V]) PortionFilled

func (c *Cache[K, V]) PortionFilled() float64

PortionFilled returns fraction of cache currently filled (0 --> 1)

func (*Cache[K, V]) Put

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

Put adds value to cache

func (*Cache[K, V]) Size

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

Size returns cache size

type SizedCache

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

SizedCache is an LRU cache bounded by total size rather than entry count.

func NewSizedCache

func NewSizedCache[K comparable, V any](maxSize int, sizeFn func(K, V) int) *SizedCache[K, V]

NewSizedCache creates a size-bounded LRU cache.

func (*SizedCache[K, V]) Evict

func (c *SizedCache[K, V]) Evict(key K)

Evict removes a key from the cache.

func (*SizedCache[K, V]) Flush

func (c *SizedCache[K, V]) Flush()

Flush removes all entries.

func (*SizedCache[K, V]) Get

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

Get retrieves a value and marks it as most recently used.

func (*SizedCache[K, V]) Len

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

Len returns number of entries.

func (*SizedCache[K, V]) PortionFilled

func (c *SizedCache[K, V]) PortionFilled() float64

PortionFilled returns the ratio of size used to max size.

func (*SizedCache[K, V]) Put

func (c *SizedCache[K, V]) Put(key K, value V)

Put inserts or replaces a value.

Jump to

Keyboard shortcuts

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