synccache

package
v0.8.18 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package synccache implements a concurrent-safe unbounded cache that evaluates and stores the result of a value loader.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyExists = errors.New("already exists")

ErrAlreadyExists is returned by Cache.Store when a key is already present in the cache.

Functions

This section is empty.

Types

type Cache

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

Cache is an object which can be used to create singletons stored in a key-value store.

func NewCache

func NewCache[K comparable, V any]() *Cache[K, V]

NewCache creates an empty unbounded Cache.

func (*Cache[K, V]) Load

func (c *Cache[K, V]) Load(ctx context.Context, key K, loader Loader[V]) (V, error)

Load executes loader for key, if a value hasn't already been loaded.

Loader is shared: concurrent callers for the same key wait on a single loader call, and the loader survives any individual caller's cancellation — it is abandoned only once every caller sharing it has gone away. A caller whose own context is still live is never handed someone else's cancellation; it starts a fresh loader instead.

func (*Cache[K, V]) Store

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

Store stores a value for a given key.

type Loader

type Loader[V any] func(ctx context.Context) (V, error)

Loader is a func that is used to load a cache value.

Jump to

Keyboard shortcuts

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