Skip to content

Commit

Permalink
feat: use route cache in alerts filter expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
sloanelybutsurely committed Aug 16, 2024
1 parent 22c777e commit 00b4c00
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ config :screens, Screens.ScreenApiResponseCache,
allocated_memory: 250_000_000

config :screens, Screens.Stops.StopsToRoutes, adapter: Nebulex.Adapters.Local
config :screens, Screens.Routes.RoutesCache, adapter: Nebulex.Adapters.Local

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ config :screens,
blue_bikes_station_status_url: [:no_api_requests_allowed_during_testing],
blue_bikes_api_client: Screens.BlueBikes.FakeClient,
stops_to_routes_route_mod: Screens.Routes.Route.Mock,
alerts_cache_filter_route_mod: Screens.Routes.Route.Mock,
routes_cache_route_mod: Screens.Routes.Route.Mock,
dup_headsign_replacements: %{
"Test 1" => "T1"
},
Expand Down Expand Up @@ -164,3 +164,4 @@ config :screens, :screens_by_alert,
screens_ttl_seconds: 1

config :screens, Screens.Stops.StopsToRoutes, adapter: Nebulex.Adapters.Nil
config :screens, Screens.Routes.RoutesCache, adapter: Nebulex.Adapters.Nil
7 changes: 3 additions & 4 deletions lib/screens/alerts/cache/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Screens.Alerts.Cache.Filter do
Logic to apply filters to a list of `Screens.Alerts.Alert` structs.
"""
alias Screens.Routes.Route
alias Screens.Routes.RoutesCache
alias Screens.RouteType
alias Screens.Stops.StopsToRoutes

Expand Down Expand Up @@ -125,10 +126,8 @@ defmodule Screens.Alerts.Cache.Filter do
end

defp matchers_for_route_id(route_id) do
route_mod = Application.get_env(:screens, :alerts_cache_filter_route_mod, Route)

case route_mod.by_id(route_id) do
{:ok, %Route{type: type}} ->
case RoutesCache.by_id(route_id) do
%Route{type: type} ->
[
%{
route_type: RouteType.to_id(type),
Expand Down
21 changes: 13 additions & 8 deletions lib/screens/routes/routes_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Screens.Routes.RoutesCache do
"""
use Nebulex.Cache,
otp_app: :screens,
adapter: Nebulex.Adapters.Local
adapter: Application.compile_env(:screens, [__MODULE__, :adapter])

alias Screens.Routes.Route

Expand All @@ -15,15 +15,20 @@ defmodule Screens.Routes.RoutesCache do
if route = get(id) do
route
else
case Route.by_id(id) do
{:ok, %Route{} = route} ->
put(id, route, ttl: ttl())
route = fetch_by_id(id)

route
unless is_nil(route), do: put(id, route, ttl: ttl())

_ ->
nil
end
route
end
end

defp fetch_by_id(id) do
route_impl = Application.get_env(:screens, :routes_cache_route_mod, Route)

case route_impl.by_id(id) do
{:ok, %Route{} = route} -> route
_ -> nil
end
end

Expand Down

0 comments on commit 00b4c00

Please sign in to comment.