Skip to content

Commit

Permalink
Add a cache module for the API main endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vdegove committed Jan 27, 2025
1 parent 1be161f commit f60c92b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
46 changes: 46 additions & 0 deletions apps/transport/lib/transport/api_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule Transport.APICache do
@moduledoc """
A module that populates the Cachex cache for the /api/datasets endpoint ("api-datasets-index")
"""

use GenServer
require Logger

@job_delay :timer.seconds(300)
@cache_ttl :timer.seconds(600)

def start_link(_opts) do
GenServer.start_link(__MODULE__, %{})
end

def init(state) do
# initial schedule is immediate, but via the same code path,
# to ensure we jump on the data
schedule_next_occurrence(0)

{:ok, state}
end

def schedule_next_occurrence(delay \\ @job_delay) do
Process.send_after(self(), :tick, delay)
end

def handle_info(:tick, state) do
schedule_next_occurrence()
populate_cache()
{:noreply, state}
end

def populate_cache do
Logger.info("[api-cache-genserver] Populating cache for /api/datasets…")

Cachex.put(
Transport.Application.cache_name(),
"api-datasets-index",
TransportWeb.API.DatasetController.prepare_datasets_index_data(),
ttl: @cache_ttl
)

Logger.info("[api-cache-genserver] Finished populating cache for /api/datasets")
end
end
5 changes: 3 additions & 2 deletions apps/transport/lib/transport/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Transport.Application do
end
end

run_realtime_poller = webserver_enabled?() && Mix.env() != :test
run_web_processes = webserver_enabled?() && Mix.env() != :test

children =
[
Expand All @@ -43,7 +43,8 @@ defmodule Transport.Application do
Transport.Vault
]
|> add_scheduler()
|> add_if(fn -> run_realtime_poller end, Transport.RealtimePoller)
|> add_if(fn -> run_web_processes end, Transport.RealtimePoller)
|> add_if(fn -> run_web_processes end, Transport.APICache)
## manually add a children supervisor that is not scheduled
|> Kernel.++([{Task.Supervisor, name: ImportTaskSupervisor}])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule TransportWeb.API.DatasetController do
# The default (one minute) felt a bit too high for someone doing scripted operations
# (have to wait during experimentations), so I lowered it a bit. It is high enough
# that it will still protect a lot against excessive querying.
@cache_ttl :timer.seconds(30)
@cache_ttl :timer.seconds(600)

@spec open_api_operation(any) :: Operation.t()
def open_api_operation(action), do: apply(__MODULE__, :"#{action}_operation", [])
Expand Down

0 comments on commit f60c92b

Please sign in to comment.