lru

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example
cache := NewCache[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 Cache

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

LRU cache. Not thread-safe.

func NewCache

func NewCache[K comparable, V any](capacity int, evicted ...func(key K, val V)) *Cache[K, V]

func (*Cache[K, V]) Cap

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

func (*Cache[K, V]) Get

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

func (*Cache[K, V]) GetOrSet

func (c *Cache[K, V]) GetOrSet(key K, setter func(K) (V, error)) (val V, err error)

func (*Cache[K, V]) Has

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

func (*Cache[K, V]) Iterate

func (c *Cache[K, V]) Iterate() iter.Seq2[K, V]

Iterate all items in no particular order.

func (*Cache[K, V]) IterateAsc

func (c *Cache[K, V]) IterateAsc() iter.Seq2[K, V]

Iterate all items in ascending order.

func (*Cache[K, V]) IterateDesc

func (c *Cache[K, V]) IterateDesc() iter.Seq2[K, V]

Iterate all items in descending order.

func (*Cache[K, V]) Len

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

func (*Cache[K, V]) Remove

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

func (*Cache[K, V]) RemoveAll

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

Clear cache and notify each evict. To clear cache without notice, use Reset.

func (*Cache[K, V]) Replace

func (c *Cache[K, V]) Replace(key K, val V) (existed bool)

func (*Cache[K, V]) Reset

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

Clear cache without notice. To clear cache and notify each evict, use RemoveAll.

func (*Cache[K, V]) Resize

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

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, val V) (ok bool)

Jump to

Keyboard shortcuts

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