cache

package
v0.10.0-dev.38 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cache is a bounded TTL cache for provider lookups that coalesces concurrent fetches of one key. A found value is kept 6 hours and an empty answer 15 minutes, so a title a provider lacks today is asked again soon; each cache holds at most 4,096 entries, so its memory does not grow with the library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NonEmpty

func NonEmpty[E any](s []E) bool

NonEmpty is New's found for a slice: a hit holds at least one element.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache maps keys to fetched values; the zero value is not usable, New makes one.

func New

func New[T any](found func(T) bool) *Cache[T]

New makes a cache; found tells a hit from an empty answer.

func (*Cache[T]) GetOrFetch

func (c *Cache[T]) GetOrFetch(ctx context.Context, key string, fn func(context.Context) (T, error)) (T, error)

GetOrFetch returns key's live value, else fn's, which it keeps when fn succeeds; an error is never kept. Concurrent calls for one key share one fn, run with the first caller's context values. A caller whose ctx ends first gets ctx's error while the others keep waiting; fn's context ends only when every caller waiting on it has left, and a later call then starts afresh. A panic in fn is kept nowhere: every caller still waiting re-panics with an error carrying fn's panic value and stack, and with none left it is logged.

Jump to

Keyboard shortcuts

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