-
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.
Cache préemptif des statistiques (#4435)
Co-authored-by: Antoine Augusti <[email protected]>
- Loading branch information
1 parent
8d1a169
commit 851e4f0
Showing
7 changed files
with
113 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Transport.PreemptiveBaseCache do | ||
@moduledoc """ | ||
Common code for preemptive caches. This module is a macro that generates a GenServer, | ||
which will populate a cache at regular intervals (similar to a cron job). | ||
Usage: | ||
``` | ||
use Transport.PreemptiveBaseCache, | ||
first_run: 0, | ||
job_delay: :timer.seconds(300), | ||
cache_ttl: :timer.seconds(700) | ||
``` | ||
The module in which it is used must implement the `populate_cache/0` function, that will be regularly called. | ||
- First run indicates the time to wait before the first run of the job between the start of the application and the first run. | ||
- Job delay indicates the time to wait between each run of the job. | ||
- Cache TTL indicates the time to keep the cache alive. | ||
""" | ||
defmacro __using__(opts) do | ||
quote do | ||
use GenServer | ||
|
||
@first_run unquote(opts[:first_run]) | ||
@job_delay unquote(opts[:job_delay]) | ||
@cache_ttl unquote(opts[:cache_ttl]) | ||
|
||
def cache_ttl, do: @cache_ttl | ||
|
||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, %{}) | ||
end | ||
|
||
def init(state) do | ||
schedule_next_occurrence(@first_run) | ||
|
||
{:ok, state} | ||
end | ||
|
||
def schedule_next_occurrence(delay) do | ||
Process.send_after(self(), :tick, delay) | ||
end | ||
|
||
def handle_info(:tick, state) do | ||
schedule_next_occurrence(@job_delay) | ||
populate_cache() | ||
{:noreply, state} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Transport.PreemptiveStatsCache do | ||
@moduledoc """ | ||
A module that populates the Cachex cache for the /stats and /api/stats/* endpoints | ||
""" | ||
|
||
use Transport.PreemptiveBaseCache, | ||
# Let’s give some time for the system to boot up before we start and the API cache to be populated | ||
first_run: :timer.minutes(1), | ||
job_delay: :timer.hours(3), | ||
# more than twice job_delay to reduce the risk of parallel computation | ||
cache_ttl: :timer.hours(7) | ||
|
||
require Logger | ||
|
||
def populate_cache do | ||
Logger.info("[preemptive-stats-cache] Populating cache for stats…") | ||
Transport.Cache.put("stats-page-index", Transport.StatsHandler.compute_stats(), @cache_ttl) | ||
|
||
Transport.Cache.put( | ||
"api-stats-aoms", | ||
TransportWeb.API.StatsController.rendered_geojson(:aoms, timeout: :timer.seconds(60)), | ||
@cache_ttl | ||
) | ||
|
||
Transport.Cache.put( | ||
"api-stats-regions", | ||
TransportWeb.API.StatsController.rendered_geojson(:regions, timeout: :timer.seconds(60)), | ||
@cache_ttl | ||
) | ||
|
||
Transport.Cache.put( | ||
"api-stats-quality", | ||
TransportWeb.API.StatsController.rendered_geojson(:quality, timeout: :timer.seconds(60)), | ||
@cache_ttl | ||
) | ||
|
||
Logger.info("[preemptive-stats-cache] Finished populating cache for stats.") | ||
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
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