cache

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache is a session-scoped, in-memory TTL memoizer for slow keyed lookups - the suggestion queries and per-key option lists that would otherwise re-hit a remote source on every keystroke or reopen. A Get within the TTL window reuses the last good result for that key; past expiry it refetches. Values are keyed and cached independently, and errors are never cached so a transient failure retries. Nothing here touches disk and nothing survives the process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TTL

type TTL[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TTL memoizes a slow, keyed lookup for a TTL window so repeated queries reuse the last good result instead of re-hitting the source.

func New

func New[K comparable, V any](
	ttl time.Duration,
	source func(context.Context, K) (V, error),
	now func() time.Time,
) *TTL[K, V]

New builds a TTL cache over source. now supplies the clock; pass nil to default to time.Now (tests inject a fake clock).

func (*TTL[K, V]) Get

func (c *TTL[K, V]) Get(ctx context.Context, key K) (V, error)

Get returns the value for key: a cached value still within its TTL, or a fresh source call whose result is cached before return.

Jump to

Keyboard shortcuts

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