cache

package
v0.1.147 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: GPL-2.0, GPL-3.0 Imports: 4 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).

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.

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

func (*Cache[T]) Reap

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

Reap removes expired entries.

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