Documentation
¶
Overview ¶
Package cache provides a generic TTL cache with singleflight coalescing.
Index ¶
- Constants
- type Cache
- func (c *Cache[T]) Clear()
- func (c *Cache[T]) Get(key string) (T, bool)
- func (c *Cache[T]) GetOrFetch(key string, fn func() (T, error)) (T, error)
- func (c *Cache[T]) GetOrFetchCtx(ctx context.Context, key string, fn func(context.Context) (T, error)) (T, error)
- func (c *Cache[T]) Reap()
- func (c *Cache[T]) Set(key string, value T)
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).
func (*Cache[T]) Get ¶
Get returns the cached value for key, or the zero value if the key is absent or expired.
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.
func (*Cache[T]) GetOrFetchCtx ¶
func (c *Cache[T]) GetOrFetchCtx(ctx context.Context, key string, fn func(context.Context) (T, error)) (T, error)
GetOrFetchCtx is like GetOrFetch but accepts a context-aware fetch function. Each caller's context is respected independently: if one caller's context is cancelled, other coalesced waiters are not affected. The fetch itself runs with a detached context so it completes for all waiters.