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 ¶
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 (*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.