PushX.JWTCache (PushX v0.11.0)

Copy Markdown View Source

Cache for short-lived bearer tokens (currently APNS JWTs).

Reads are lock-free via a public ETS table. On a cache miss, refresh is serialized through this GenServer to prevent a thundering-herd of JWT generations. If the GenServer crashes, its supervisor restarts it and the next call regenerates the token — there is no shared mutex that can be left held, so a killed refresher cannot deadlock the cache.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns a cached token if still fresh, otherwise asks the cache process to generate a new one via generate_fn and caches it for ttl_ms.

Removes a cached entry. Used when an instance is stopped.

Types

cache_key()

@type cache_key() :: term()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_or_generate(cache_key, generate_fn, ttl_ms)

@spec get_or_generate(
  cache_key(),
  (-> {:ok, String.t()} | {:error, term()}),
  pos_integer()
) ::
  {:ok, String.t()} | {:error, term()}

Returns a cached token if still fresh, otherwise asks the cache process to generate a new one via generate_fn and caches it for ttl_ms.

generate_fn must return {:ok, token} or {:error, reason}.

invalidate(cache_key)

@spec invalidate(cache_key()) :: :ok

Removes a cached entry. Used when an instance is stopped.