lazycache

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 2 Imported by: 13

README

Tests on Linux, MacOS and Windows Go Report Card codecov GoDoc

Lazycache is a simple thread safe in-memory LRU cache. Under the hood it leverages the great simpleru package in golang-lru, with its exellent performance. One big difference between golang-lru and this library is the GetOrCreate method, which provides:

  • Non-blocking cache priming on cache misses.
  • A guarantee that the prime function is only called once for a given key.
  • The cache's RWMutex is not locked during the execution of the prime function, which should make it easier to reason about potential deadlocks.

Other notable features:

  • The API is generic
  • The cache can be resized while running.
  • When the number of entries overflows the defined cache size, the least recently used item gets discarded (LRU).

Documentation

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 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]) 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.

Jump to

Keyboard shortcuts

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