Skip to content

Commit

Permalink
Merge pull request #7 from WISVCH/caching-issue
Browse files Browse the repository at this point in the history
Fix caching issue
  • Loading branch information
KasperVaessen authored Dec 8, 2024
2 parents 151417b + 6a18a55 commit 5e8e5eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
50 changes: 31 additions & 19 deletions lib/aoch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions lib/aoch_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 5e8e5eb

Please sign in to comment.