cache

package
v0.69.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache adds a session-partitioned, in-memory token cache in front of any exchange.TokenExchanger.

Cache decorates a provider (entra.Exchanger today) and implements exchange.Exchanger, so the proxy middleware and the vMCP strategy are unaware of the caching. Cached tokens are partitioned by (hash(userToken), audience): a given user's token for a given audience is reused until it is within a configurable skew of its expiry, at which point the next call re-exchanges. Different user tokens and different audiences never collide.

Typical wiring:

exchanger, err := entra.NewFromTokenURL(tokenURL, clientID, clientSecret, nil)
if err != nil { ... }
var oboExchanger exchange.Exchanger = cache.New(exchanger)

The package links only the standard library and the stdlib-only pkg/auth/obo/exchange contract.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache decorates an exchange.TokenExchanger with a session-partitioned, in-memory token cache and implements exchange.Exchanger. It is safe for concurrent use.

Concurrent misses for the same (userToken, audience) are coalesced by single-flight: only one underlying exchange runs at a time per key, and the other callers wait for and share its result. This keeps a burst of requests for one session — the common case when a cached token reaches its expiry — from stampeding the token endpoint. The shared exchange runs on its own goroutine under a context detached from any single caller (context.WithoutCancel), so one caller's cancellation cannot fail the others; each caller is still released early by its own context. See fetch / runExchange.

func New

func New(delegate exchange.TokenExchanger, opts ...Option) *Cache

New returns a Cache fronting delegate. It panics if delegate is nil or if an option sets a negative skew — both are programming errors that should surface at wiring time, not on the first exchange.

func (*Cache) Exchange

func (c *Cache) Exchange(ctx context.Context, userToken, audience string) (string, error)

Exchange implements exchange.Exchanger. It returns a cached token for (userToken, audience) when one is present and not within the skew of its expiry; otherwise it delegates to the underlying TokenExchanger, caches the result (when it carries an expiry), and returns it. Concurrent misses for the same key are coalesced into a single underlying exchange.

type Option

type Option func(*Cache)

Option configures a Cache at construction time.

func WithClock

func WithClock(now func() time.Time) Option

WithClock overrides the time source, primarily for deterministic tests. A nil argument is ignored and the default (time.Now) is kept.

func WithSkew

func WithSkew(skew time.Duration) Option

WithSkew overrides the expiry skew (default 60s). A token is treated as stale once the current time is within skew of its expiry. A negative skew panics.

Jump to

Keyboard shortcuts

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