cache

package
v0.10.0-dev.23 Latest Latest
Warning

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

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

Documentation

Overview

Package cache provides a generic TTL cache with singleflight coalescing.

Index

Constants

View Source
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 New

func New[T any](ttl time.Duration) *Cache[T]

New creates a cache with the given TTL.

func (*Cache[T]) Clear

func (c *Cache[T]) Clear()

Clear removes all entries.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

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

func (c *Cache[T]) GetOrFetch(key string, fn func() (T, error)) (T, error)

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]) Set

func (c *Cache[T]) Set(key string, value T)

Set stores a value with the cache's TTL.

Jump to

Keyboard shortcuts

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