lru

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: MIT Imports: 4 Imported by: 1

README

lru

Thread-safe LRU Cache Implementation in Golang Generic

Go Report Card GoDoc GitHub license gocover.io

License

Licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU is a thread-safe fixed size LRU cache.

func NewLRU

func NewLRU[K comparable, V any](size int, evicted func(K, V)) *LRU[K, V]

NewLRU returns a new LRU of the given size.

func (*LRU[K, V]) Cap

func (l *LRU[K, V]) Cap() int

Cap returns the capacity of the lru cache

func (*LRU[K, V]) Close

func (l *LRU[K, V]) Close()

Close closes the cache.

func (*LRU[K, V]) Contains

func (l *LRU[K, V]) Contains(key K) bool

Contains returns true if the key exists.

func (*LRU[K, V]) Delete

func (l *LRU[K, V]) Delete(key K) (prev V, deleted bool)

Delete a value for a key

func (*LRU[K, V]) Evict

func (l *LRU[K, V]) Evict() (key K, value V, evicted bool)

Evict evicts the least recently used item from the cache.

func (*LRU[K, V]) ForEach

func (l *LRU[K, V]) ForEach(iter func(key K, value V) bool)

ForEach iterates over the cache, calling f for each item.

func (*LRU[K, V]) Get

func (l *LRU[K, V]) Get(key K) (value V, ok bool)

Get returns a value for key and mark it as most recently used.

func (*LRU[K, V]) Keys

func (l *LRU[K, V]) Keys() []K

Keys returns a slice of the keys in the cache.

func (*LRU[K, V]) Len

func (l *LRU[K, V]) Len() int

Len returns the length of the lru cache

func (*LRU[K, V]) Peek

func (l *LRU[K, V]) Peek(key K) (value V, ok bool)

Peek returns the value for key without marking it as most recently used.

func (*LRU[K, V]) Put

func (l *LRU[K, V]) Put(key K, value V) (prev V, replaced bool)

Put sets the value for the specified key.

func (*LRU[K, V]) Resize

func (l *LRU[K, V]) Resize(size int)

Resize resizes the cache to the specified size.

func (*LRU[K, V]) Values

func (l *LRU[K, V]) Values() []V

Values returns a slice of the values in the cache.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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