Documentation
¶
Overview ¶
Package cache provides a generic TTL cache with singleflight coalescing.
Index ¶
Constants ¶
const DefaultTTL = 6 * time.Hour
DefaultTTL is the standard TTL for provider lookup caches. Used by providers that cache title/show ID lookups across scan cycles.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache is a generic TTL cache for provider lookups. Thread-safe. Used to avoid redundant API calls when scanning multiple episodes of the same series (e.g. title ID lookups, torrent ID lookups).
The zero value is NOT usable: always construct with New. Two things are missing from it, and only the first is a crash. The entries map is nil, so Set (and every path through it, GetOrFetch included) panics on the write; Get and Clear happen to tolerate a nil map, which is what makes the zero value look serviceable until the first store. The ttl is also 0, so even with a map every entry would expire the instant it was written. A zero ttl is meaningful on its own — New(0) is a legal call that yields exactly that expire-immediately cache — so it is not reinterpreted here as a default or as "never expires", and the zero value cannot be rescued by giving it a second meaning.
func (*Cache[T]) Get ¶
Get returns the cached value for key, or the zero value if the key is absent or expired. Get is read-only: an expired entry is reported as a miss but stays in the map until it is overwritten or Clear reclaims it.
func (*Cache[T]) GetOrFetch ¶
GetOrFetch returns the cached value for key, or calls fn to fetch it. Concurrent calls for the same key are coalesced via singleflight.