-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache routes by id to speed up alerts filtering (#2142)
* feat: cache routes * feat: use route cache in alerts filter expansion
- Loading branch information
1 parent
f07d595
commit 6f88363
Showing
5 changed files
with
47 additions
and
6 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
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,39 @@ | ||
defmodule Screens.Routes.RoutesCache do | ||
@moduledoc """ | ||
A read-through cache of routes by ID. | ||
""" | ||
use Nebulex.Cache, | ||
otp_app: :screens, | ||
adapter: Application.compile_env(:screens, [__MODULE__, :adapter]) | ||
|
||
alias Screens.Routes.Route | ||
|
||
@route_mod Application.compile_env(:screens, :routes_cache_route_mod, Screens.Routes.Route) | ||
|
||
@base_ttl :timer.hours(1) | ||
|
||
@spec by_id(id :: String.t()) :: Route.t() | nil | ||
def by_id(id) do | ||
if route = get(id) do | ||
route | ||
else | ||
route = fetch_by_id(id) | ||
|
||
unless is_nil(route), do: put(id, route, ttl: ttl()) | ||
|
||
route | ||
end | ||
end | ||
|
||
defp fetch_by_id(id) do | ||
case @route_mod.by_id(id) do | ||
{:ok, %Route{} = route} -> route | ||
_ -> nil | ||
end | ||
end | ||
|
||
def ttl do | ||
additional_minutes = :rand.uniform(30) | ||
@base_ttl + :timer.minutes(additional_minutes) | ||
end | ||
end |