diff --git a/lib/aoch.ex b/lib/aoch.ex index bc1cc21..c828279 100644 --- a/lib/aoch.ex +++ b/lib/aoch.ex @@ -8,18 +8,35 @@ defmodule AoCH do """ def get_leaderboard_day(day, year) do - {leaderboard_day, _, _} = get_data(year, day) - leaderboard_day + raw_data = get_data(year) + + ConCache.get_or_store(:cache, "leaderboard_day_#{year}_#{day}", fn -> + parse_leaderboard_day(raw_data, day, year) + end) end - def get_leaderboard_year(day, year) do - {_, leaderboard, _} = get_data(year, day) - leaderboard + def get_leaderboard_year(year) do + raw_data = get_data(year) + + ConCache.get_or_store(:cache, "leaderboard_#{year}", fn -> + parse_leaderboard_year(raw_data) + end) end def get_api_data(day, year) do - {_, _, data} = get_data(year, day) - data + raw_data = get_data(year) + + ConCache.get_or_store(:cache, "data_#{year}_#{day}", fn -> + leaderboard_day = parse_leaderboard_day(raw_data, day, year) + leaderboard = parse_leaderboard_year(raw_data) + day_challenge = get_challenge(day, year) + + %{ + assignment: day_challenge, + today: leaderboard_day |> Enum.map(&assign_time_strings/1), + total: leaderboard + } + end) end def get_challenge(day, year) do @@ -47,21 +64,16 @@ defmodule AoCH do } end - defp get_data(year, day) do + defp get_data(year) do ConCache.get_or_store(:cache, "data_#{year}", fn -> - raw_data = request_raw_data(year) - leaderboard_day = parse_leaderboard_day(raw_data, day, year) - leaderboard = parse_leaderboard_year(raw_data) - day_challenge = get_challenge(day, year) + for day <- 1..25 do + ConCache.delete(:cache, "leaderboard_day_#{year}_#{day}") + ConCache.delete(:cache, "data_#{year}_#{day}") + end - data = - %{ - assignment: day_challenge, - today: leaderboard_day |> Enum.map(&assign_time_strings/1), - total: leaderboard - } + ConCache.delete(:cache, "leaderboard_#{year}") - {leaderboard_day, leaderboard, data} + request_raw_data(year) end) end diff --git a/lib/aoch_web/controllers/page_controller.ex b/lib/aoch_web/controllers/page_controller.ex index 77e9ae7..7105a84 100644 --- a/lib/aoch_web/controllers/page_controller.ex +++ b/lib/aoch_web/controllers/page_controller.ex @@ -4,7 +4,7 @@ defmodule AoCHWeb.PageController do def today(conn, params) do now = AoCH.now() year = to_int(params["year"]) || if now.month == 12, do: now.year, else: now.year - 1 - day = to_int(params["day"]) || if now.month < 12, do: 25, else: now.day + day = to_int(params["day"]) || if now.month < 12 or now.day > 25, do: 25, else: now.day today = AoCH.get_leaderboard_day(day, year) @@ -19,8 +19,7 @@ defmodule AoCHWeb.PageController do def total(conn, params) do now = AoCH.now() year = to_int(params["year"]) || if now.month == 12, do: now.year, else: now.year - 1 - day = to_int(params["day"]) || if now.month < 12, do: 25, else: now.day - total = AoCH.get_leaderboard_year(day, year) + total = AoCH.get_leaderboard_year(year) render(conn, :total, year: year, @@ -33,7 +32,7 @@ defmodule AoCHWeb.PageController do def challenge(conn, params) do now = AoCH.now() year = to_int(params["year"]) || if now.month == 12, do: now.year, else: now.year - 1 - day = to_int(params["day"]) || if now.month < 12, do: 25, else: now.day + day = to_int(params["day"]) || if now.month < 12 or now.day > 25, do: 25, else: now.day assignment = AoCH.get_challenge(day, year) render(conn, :assignment,