-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cache module for the API main endpoint
- Loading branch information
Showing
3 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters