Stevedore.Auth.Cache (Stevedore v0.2.0)

Copy Markdown View Source

An opt-in, in-process cache of registry bearer tokens, keyed by {registry, scope}.

By default Stevedore.Registry re-runs the 401 → token handshake on every request. Start a cache and pass it as the :token_cache option to reuse a token across the manifest + blob fetches of a pull: the first request earns the token, the rest send it preemptively — skipping both the 401 and the token-endpoint round-trip. A stale or rejected token still falls back to a fresh handshake, so the cache never changes results, only request count.

Tokens are cached for :ttl milliseconds (default 60s, comfortably inside a typical registry token lifetime); the 401 fallback covers any token that expires sooner. Starting a cache is the consumer's choice — nothing here runs unless you start it, preserving Stevedore's weightless-by-default invariant.

Example

{:ok, cache} = Stevedore.Auth.Cache.start_link([])
Stevedore.copy("docker://alpine:3.20", "oci:./alpine:3.20", token_cache: cache)

Summary

Types

A cache entry's key: the registry host and the auth scope the token is valid for.

Functions

Returns a specification to start this module under a supervisor.

Drops all cached tokens.

Returns {:ok, token} for key, or :miss when it is absent or expired.

Starts a token cache.

Types

key()

@type key() :: {registry :: String.t(), scope :: String.t()}

A cache entry's key: the registry host and the auth scope the token is valid for.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear(server)

@spec clear(Agent.agent()) :: :ok

Drops all cached tokens.

get(server, key)

@spec get(Agent.agent(), key()) :: {:ok, String.t()} | :miss

Returns {:ok, token} for key, or :miss when it is absent or expired.

put(server, key, token, ttl \\ :default)

@spec put(Agent.agent(), key(), String.t(), non_neg_integer() | :default) :: :ok

Caches token under key.

ttl is the lifetime in milliseconds, or :default to use the cache's configured :ttl.

start_link(opts \\ [])

@spec start_link(keyword()) :: Agent.on_start()

Starts a token cache.

Options: :name (register the process under a name) and :ttl (token lifetime in milliseconds, default 60000). Other options are passed to Agent.start_link/2.