PushX.JWTCache (PushX v0.15.0)

Copy Markdown View Source

Cache for short-lived, expensively-derived values: APNS JWTs, per-origin VAPID JWTs and resolved VAPID key pairs.

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.

Removes every entry whose key matches an ETS match pattern, e.g. {:vapid_jwt, :tenant_web, :_} for all of an instance's per-origin VAPID JWTs. Used when an instance is stopped or reconfigured.

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, term()} | {:error, term()}),
  pos_integer()
) ::
  {:ok, term()} | {: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, value} or {:error, reason}; only {:ok, _} results are cached.

invalidate(cache_key)

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

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

invalidate_match(pattern)

@spec invalidate_match(tuple()) :: :ok

Removes every entry whose key matches an ETS match pattern, e.g. {:vapid_jwt, :tenant_web, :_} for all of an instance's per-origin VAPID JWTs. Used when an instance is stopped or reconfigured.