An ETS-backed cache for compiled format patterns.
Number format metadata and datetime format tokens are cached here after first compilation. The cache is hard-bounded: when inserting an entry would exceed the configured maximum, an existing entry is evicted synchronously, keeping the cache at or below the cap at all times.
The maximum number of entries defaults to 2,000 and can be overridden with:
config :localize, :format_cache_max_entries, 5_000Trust model
The ETS table is :protected — only the cache GenServer can
write to it; any process can read directly. This keeps the
cache from being polluted by other libraries running in the
same BEAM, and ensures the size invariant cannot be violated
by a non-owner write.
Writes go through store/2, which is a GenServer.call to the
owner. A miss-then-store pattern from a hot path therefore pays
one gen-server round-trip per first-time format compilation;
subsequent lookups are direct ETS reads with no synchronisation
cost.
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears all entries from the cache.
Look up a compiled format pattern by its cache key.
Returns the configured maximum number of cache entries.
Returns the current number of entries in the cache.
Store a compiled format pattern in the cache.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear() :: :ok
Clears all entries from the cache.
Routed through the GenServer so the operation respects the
table's :protected ownership. Intended for tests and
maintenance; production callers should not need this.
Look up a compiled format pattern by its cache key.
Arguments
keyis the cache key, typically a tuple like{:localize, :number_format_meta, format_string}.
Returns
{:ok, value}if the key is present.:missif not cached or the table does not exist.
@spec max_entries() :: pos_integer()
Returns the configured maximum number of cache entries.
@spec size() :: non_neg_integer()
Returns the current number of entries in the cache.
Primarily useful in tests; production callers should not need to inspect the size directly.
Store a compiled format pattern in the cache.
Routed through the cache GenServer so the size cap can be enforced synchronously. If the table doesn't exist (e.g. during a bare unit test), the call is a no-op.
Arguments
keyis the cache key.valueis the compiled artifact to cache.
Returns
:ok.