prop/lib/microwaveprop/cache.ex
Graham McIntire 6c334d6e18 Cache /contacts, fix map blink, make mode optional
- Add contacts_inserted_at_desc_id_desc_idx so /contacts list runs as an
  Index Scan (~0.4ms) instead of a Seq Scan + top-N heapsort (~77ms on
  58k rows)
- Add Microwaveprop.Cache: tiny ETS-backed TTL cache for memoizing
  expensive-but-stale-tolerant values
- Cache total_entries for unsearched /contacts page loads (30s TTL,
  invalidated on insert)
- Cache the entire /contacts/map payload (pre-encoded JSON + band list),
  moving load_contacts into Radio.contact_map_payload; invalidated on
  new contact insert. Mount goes from ~150-300ms to ~5ms.
- Fix /map overlay blinking on load: reuse the Leaflet GridLayer across
  renderScores calls and just redraw() against the updated ScoreGrid,
  instead of removeLayer + new layer which flashed between tile regens
- Make mode optional on submission: schema change (null: true),
  submission_changeset no longer requires it, blank strings normalise to
  nil, CSV import accepts 6-column (no mode) or 7-column layouts
- core_components .input adds a red asterisk to labels when required,
  giving users a visual cue for which fields must be filled on /submit
2026-04-12 12:47:25 -05:00

71 lines
1.9 KiB
Elixir

defmodule Microwaveprop.Cache do
@moduledoc """
Tiny ETS-backed TTL cache for values that are expensive to compute but
tolerate short staleness. Used for things like `Repo.aggregate` counts that
would otherwise run on every page load.
Not a replacement for `Microwaveprop.Propagation.ScoreCache` or
`Microwaveprop.Weather.NexradCache` — those have bespoke invalidation logic
driven by PubSub. This module is for generic time-boxed memoization.
"""
use GenServer
@table :microwaveprop_cache
@spec start_link(keyword()) :: GenServer.on_start()
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@doc """
Return the cached value for `key` if it's still fresh, otherwise invoke
`fun`, store the result with `ttl_ms` lifetime, and return it.
"""
@spec fetch_or_store(term(), non_neg_integer(), (-> value)) :: value when value: term()
def fetch_or_store(key, ttl_ms, fun) when is_function(fun, 0) do
now = System.monotonic_time(:millisecond)
case :ets.lookup(@table, key) do
[{_, value, expires_at}] when expires_at > now ->
value
_ ->
value = fun.()
:ets.insert(@table, {key, value, now + ttl_ms})
value
end
end
@doc "Insert `value` directly, overwriting any existing entry for `key`."
@spec put(term(), term(), integer()) :: :ok
def put(key, value, ttl_ms) do
:ets.insert(@table, {key, value, System.monotonic_time(:millisecond) + ttl_ms})
:ok
end
@doc "Remove `key` from the cache, forcing the next fetch to recompute."
@spec invalidate(term()) :: :ok
def invalidate(key) do
:ets.delete(@table, key)
:ok
end
@spec clear() :: :ok
def clear do
:ets.delete_all_objects(@table)
:ok
end
@impl true
def init(_opts) do
:ets.new(@table, [
:set,
:named_table,
:public,
read_concurrency: true,
write_concurrency: true
])
{:ok, %{}}
end
end