lazycache – github.com/bep/lazycache Index | Files

package lazycache

import "github.com/bep/lazycache"

Index

Types

type Cache

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

Cache is a thread-safe resizable LRU cache.

func New

func New[K comparable, V any](options Options[K, V]) *Cache[K, V]

New creates a new Cache.

func (*Cache[K, V]) Delete

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

Delete deletes the item with given key from the cache, returning if the key was contained.

func (*Cache[K, V]) DeleteFunc

func (c *Cache[K, V]) DeleteFunc(matches func(key K, item V) bool) int

DeleteFunc deletes all entries for which the given function returns true.

func (*Cache[K, V]) Get

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

Get returns the value associated with key.

func (*Cache[K, V]) GetOrCreate

func (c *Cache[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V, bool, error)

GetOrCreate returns the value associated with key, or creates it if it doesn't. It also returns a bool indicating if the value was found in the cache. Note that create, the cache prime function, is called once and then not called again for a given key unless the cache entry is evicted; it does not block other goroutines from calling GetOrCreate, it is not called with the cache lock held. Note that any error returned by create will be returned by GetOrCreate and repeated calls with the same key will receive the same error.

func (*Cache[K, V]) Len

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

Len returns the number of items in the cache. note that this wil also include values that are not yet ready.

func (*Cache[K, V]) Resize

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

Resize changes the cache size and returns the number of entries evicted.

func (*Cache[K, V]) Set

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

Set associates value with key.

type Options

type Options[K comparable, V any] struct {
	// MaxEntries is the maximum number of entries that the cache should hold.
	// Note that this can also be adjusted after the cache is created with Resize.
	MaxEntries int

	// OnEvict is an optional callback that is called when an entry is evicted.
	OnEvict func(key K, value V)
}

Options holds the cache options.

Source Files

lazycache.go

Version
v0.8.0 (latest)
Published
Feb 26, 2025
Platform
js/wasm
Imports
2 packages
Last checked
now

Tools for package owners.