Packages
localize
0.10.0
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.41.3
0.41.2
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.1
0.30.0
retired
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
0.1.0-alpha.1
Localization (parsing, formatting) of numbers, dates/time/calendar, units of measure, messages and lists. Includes localized collation.
Current section
Files
Jump to
Current section
Files
lib/localize/format_cache.ex
defmodule Localize.FormatCache do
@moduledoc """
An ETS-backed cache for compiled format patterns.
Number format metadata and datetime format tokens are cached
here after first compilation. A built-in sweeper periodically
evicts random entries when the cache exceeds its configured
maximum size.
The maximum number of entries defaults to 2,000 and can be
overridden with:
config :localize, :format_cache_max_entries, 5_000
"""
use GenServer
@table :localize_format_cache
@sweep_interval_ms 10_000
@default_max_entries 2_000
# ── Client API ──────────────────────────────────────────────
@doc """
Look up a compiled format pattern by its cache key.
### Arguments
* `key` is the cache key, typically a tuple like
`{:localize, :number_format_meta, format_string}`.
### Returns
* `{:ok, value}` if the key is present.
* `:miss` if not cached or the table does not exist.
"""
@spec lookup(term()) :: {:ok, term()} | :miss
def lookup(key) do
if :ets.whereis(@table) != :undefined do
case :ets.lookup(@table, key) do
[{^key, value}] -> {:ok, value}
[] -> :miss
end
else
:miss
end
end
@doc """
Store a compiled format pattern in the cache.
### Arguments
* `key` is the cache key.
* `value` is the compiled artifact to cache.
### Returns
* `:ok`.
"""
@spec store(term(), term()) :: :ok
def store(key, value) do
if :ets.whereis(@table) != :undefined do
:ets.insert(@table, {key, value})
end
:ok
end
# ── GenServer (sweeper) ─────────────────────────────────────
@doc false
def start_link(options) do
GenServer.start_link(__MODULE__, options, name: __MODULE__)
end
@impl true
def init(_options) do
ensure_table()
schedule_sweep()
{:ok, []}
end
@impl true
def handle_info(:sweep, state) do
sweep()
schedule_sweep()
{:noreply, state}
end
defp ensure_table do
if :ets.whereis(@table) == :undefined do
:ets.new(@table, [
:set,
:public,
:named_table,
read_concurrency: true
])
end
end
defp schedule_sweep do
Process.send_after(self(), :sweep, @sweep_interval_ms)
end
defp sweep do
if :ets.whereis(@table) != :undefined do
max_entries =
Application.get_env(:localize, :format_cache_max_entries, @default_max_entries)
size = :ets.info(@table, :size)
if size > max_entries do
evict_random(size - max_entries)
end
end
end
defp evict_random(count) when count <= 0, do: :ok
defp evict_random(count) do
key = :ets.first(@table)
evict_random(key, count)
end
defp evict_random(:"$end_of_table", _remaining), do: :ok
defp evict_random(_key, 0), do: :ok
defp evict_random(key, remaining) do
next_key = :ets.next(@table, key)
if :rand.uniform() < 0.5 do
:ets.delete(@table, key)
evict_random(next_key, remaining - 1)
else
evict_random(next_key, remaining)
end
end
end