diff --git a/config/config.exs b/config/config.exs index 762e36f1f..080d2aa16 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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. diff --git a/config/test.exs b/config/test.exs index 31ccf79b9..01855c5c4 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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" }, @@ -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 diff --git a/lib/screens/alerts/cache/filter.ex b/lib/screens/alerts/cache/filter.ex index ef6e10860..e496bab78 100644 --- a/lib/screens/alerts/cache/filter.ex +++ b/lib/screens/alerts/cache/filter.ex @@ -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 @@ -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), diff --git a/lib/screens/routes/routes_cache.ex b/lib/screens/routes/routes_cache.ex index b13320a01..e083f6b45 100644 --- a/lib/screens/routes/routes_cache.ex +++ b/lib/screens/routes/routes_cache.ex @@ -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 @@ -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