lru

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 3 Imported by: 1

README

LRU cache

A fast, generic and iterable LRU cache written in Go. Comes with a thread-safe variant.

Documentation

See: https://pkg.go.dev/github.com/webmafia/lru

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/webmafia/lru
cpu: Apple M1 Pro
Benchmark/cap_008-10      	95708524	        12.43 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_016-10      	74377090	        16.64 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_032-10      	45249507	        25.84 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_064-10      	26544806	        46.38 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_128-10      	13576695	        88.19 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_256-10      	 7849098	       152.8 ns/op	       0 B/op	       0 allocs/op
Benchmark/cap_512-10      	 4349156	       274.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_008-10         	53933190	        21.45 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_016-10         	54433185	        22.43 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_032-10         	33993650	        33.49 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_064-10         	22557008	        52.11 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_128-10         	12623346	        95.21 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_256-10         	 7475310	       160.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkThreadsafe/cap_512-10         	 4271628	       281.6 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/webmafia/lru	19.095s

Documentation

Overview

Example
cache := New[int, struct{}](8, func(key int, _ struct{}) {
	fmt.Println("evicted", key)
})

for i := 1; i <= 10; i++ {
	cache.Replace(i, struct{}{})
}

fmt.Printf("%d items in cache\n", cache.Len())

for k, v := range cache.IterateAsc() {
	fmt.Println(k, v)
}
Output:


evicted 1
evicted 2
8 items in cache
3 {}
4 {}
5 {}
6 {}
7 {}
8 {}
9 {}
10 {}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

type LRU[K comparable, V any] interface {
	Len() int
	Cap() int
	Resize(capacity int)
	Has(key K) (ok bool)
	Get(key K) (val V, ok bool)
	GetOrSet(key K, setter func(K) (V, error)) (val V, err error)
	Set(key K, val V) (ok bool)
	Replace(key K, val V) (existed bool)
	Remove(key K) (existed bool)
	Iterate() iter.Seq2[K, V]
	IterateAsc() iter.Seq2[K, V]
	IterateDesc() iter.Seq2[K, V]

	// Clear cache and notify each evict. To clear cache without notice, use Reset.
	RemoveAll()

	// Clear cache without notice. To clear cache and notify each evict, use RemoveAll.
	Reset()
}

func New

func New[K comparable, V any](capacity int, evicted ...func(key K, val V)) LRU[K, V]

func NewThreadSafe

func NewThreadSafe[K comparable, V any](capacity int, evicted ...func(key K, val V)) LRU[K, V]

Jump to

Keyboard shortcuts

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