Skip to content

Commit

Permalink
feat(GlobalDataCache): Try populating cache out of band on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
KaylaBrady committed Sep 25, 2024
1 parent 4781abb commit 011cfda
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/mobile_app_backend/global_data_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,21 @@ defmodule MobileAppBackend.GlobalDataCache.Impl do
update_ms: opts[:update_ms] || :timer.minutes(5)
}

Process.send_after(self(), :recalculate, :timer.seconds(1))

{:ok, state}
end

@impl GenServer
def handle_info(:recalculate, %State{} = state) do
def handle_info(:recalculate, %State{update_ms: update_ms} = state) do
update_data(state.key)

Process.send_after(self(), :recalculate, state.update_ms)
if :persistent_term.get(state.key, nil) do
Process.send_after(self(), :recalculate, update_ms)
else
# no data yet, try again sooner
Process.send_after(self(), :recalculate, :timer.seconds(5))
end

{:noreply, state}
end
Expand Down

0 comments on commit 011cfda

Please sign in to comment.