From 38dec7a60e6c6329b062c1d30e37fac55a86706a Mon Sep 17 00:00:00 2001 From: Ryan Mahoney Date: Thu, 5 Sep 2019 11:33:41 -0400 Subject: [PATCH] cache realtime data, reduce TNM polling time (#197) --- .../ts/tnm/components/TransitNearMe.tsx | 2 +- apps/site/lib/site/application.ex | 3 ++- apps/site/lib/site/realtime_schedule.ex | 22 +++++++++++++------ apps/site/mix.exs | 3 ++- .../site/test/site/realtime_schedule_test.exs | 1 + 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/apps/site/assets/ts/tnm/components/TransitNearMe.tsx b/apps/site/assets/ts/tnm/components/TransitNearMe.tsx index 28238388a2..aa38d61af3 100644 --- a/apps/site/assets/ts/tnm/components/TransitNearMe.tsx +++ b/apps/site/assets/ts/tnm/components/TransitNearMe.tsx @@ -148,7 +148,7 @@ const TransitNearMe = ({ stopsWithDistances.stops.map(stop => stop.id), dispatch ), - 45000 + 30000 ); return ( diff --git a/apps/site/lib/site/application.ex b/apps/site/lib/site/application.ex index 799e993be7..40d8580dd6 100644 --- a/apps/site/lib/site/application.ex +++ b/apps/site/lib/site/application.ex @@ -18,7 +18,8 @@ defmodule Site.Application do supervisor(SiteWeb.Endpoint, []), supervisor(Site.GreenLine.Supervisor, []), supervisor(Site.Stream.Vehicles, []), - supervisor(Site.React, []) + supervisor(Site.React, []), + supervisor(Site.RealtimeSchedule, []) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/apps/site/lib/site/realtime_schedule.ex b/apps/site/lib/site/realtime_schedule.ex index 6a1c1da34b..a8a4b7854c 100644 --- a/apps/site/lib/site/realtime_schedule.ex +++ b/apps/site/lib/site/realtime_schedule.ex @@ -7,6 +7,8 @@ defmodule Site.RealtimeSchedule do are considering route patterns with the same name to be effectively the same """ + use RepoCache, ttl: :timer.seconds(30) + alias Predictions.Prediction alias Predictions.Repo, as: PredictionsRepo alias RoutePatterns.RoutePattern @@ -42,6 +44,13 @@ defmodule Site.RealtimeSchedule do @spec stop_data([Stop.id_t()], DateTime.t(), Keyword.t()) :: [map] def stop_data(stop_ids, now, opts \\ []) do + cache(stop_ids, fn _ -> + do_stop_data(stop_ids, now, opts) + end) + end + + @spec do_stop_data([Stop.id_t()], DateTime.t(), Keyword.t()) :: [map] + defp do_stop_data(stop_ids, now, opts) do opts = Keyword.merge(@default_opts, opts) stops_fn = Keyword.fetch!(opts, :stops_fn) routes_fn = Keyword.fetch!(opts, :routes_fn) @@ -49,22 +58,21 @@ defmodule Site.RealtimeSchedule do schedules_fn = Keyword.fetch!(opts, :schedules_fn) alerts_fn = Keyword.fetch!(opts, :alerts_fn) - # stage 1, get stops and routes - stops_task = Task.async(fn -> get_stops(stop_ids, stops_fn) end) + # stage 1, get routes routes_task = Task.async(fn -> get_routes(stop_ids, routes_fn) end) - [stops, route_with_patterns] = Enum.map([stops_task, routes_task], &Task.await/1) + route_with_patterns = Task.await(routes_task) - # stage 2, get predictions, schedules, and alerts + # stage 2, get stops, predictions, schedules, and alerts + stops_task = Task.async(fn -> get_stops(stop_ids, stops_fn) end) predictions_task = Task.async(fn -> get_predictions(route_with_patterns, predictions_fn) end) - schedules_task = Task.async(fn -> get_schedules(route_with_patterns, now, schedules_fn) end) alert_counts_task = Task.async(fn -> get_alert_counts(route_with_patterns, now, alerts_fn) end) - [predictions, schedules, alert_counts] = + [stops, predictions, schedules, alert_counts] = Enum.map( - [predictions_task, schedules_task, alert_counts_task], + [stops_task, predictions_task, schedules_task, alert_counts_task], &Task.await(&1, @long_timeout) ) diff --git a/apps/site/mix.exs b/apps/site/mix.exs index 6a66cc81d5..27c8999db9 100644 --- a/apps/site/mix.exs +++ b/apps/site/mix.exs @@ -126,7 +126,8 @@ defmodule Site.Mixfile do {:predictions, in_umbrella: true}, {:trip_plan, in_umbrella: true}, {:services, in_umbrella: true}, - {:route_patterns, in_umbrella: true} + {:route_patterns, in_umbrella: true}, + {:repo_cache, in_umbrella: true} ] end end diff --git a/apps/site/test/site/realtime_schedule_test.exs b/apps/site/test/site/realtime_schedule_test.exs index bd37e3e5cc..d1e4e5cd60 100644 --- a/apps/site/test/site/realtime_schedule_test.exs +++ b/apps/site/test/site/realtime_schedule_test.exs @@ -252,6 +252,7 @@ defmodule Site.RealtimeScheduleTest do } ] + RealtimeSchedule.clear_cache() actual = RealtimeSchedule.stop_data(stops, @now, opts) assert actual == expected