From 4fd053bc39fcf1643923d10a0e60d9bb84f748d3 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 17 Jun 2019 14:41:42 +0300 Subject: [PATCH] use cached data --- .../chain/market_history_chart_controller.ex | 16 ++++---- .../lib/block_scout_web/notifier.ex | 2 +- apps/explorer/lib/explorer/chain.ex | 3 +- apps/explorer/lib/explorer/market/market.ex | 19 ++-------- .../explorer/market/market_history_cache.ex | 4 ++ .../market/market_history_cache_test.exs | 37 +++++++++++++++++++ 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/chain/market_history_chart_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/chain/market_history_chart_controller.ex index 4a498b8430..a89728949c 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/chain/market_history_chart_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/chain/market_history_chart_controller.ex @@ -8,18 +8,20 @@ defmodule BlockScoutWeb.Chain.MarketHistoryChartController do with true <- ajax?(conn) do exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() + recent_market_history = Market.fetch_recent_history() + market_history_data = - 30 - |> Market.fetch_recent_history() - |> case do - [today | the_rest] -> [%{today | closing_price: exchange_rate.usd_value} | the_rest] - data -> data + case recent_market_history do + [today | the_rest] -> + encode_market_history_data([%{today | closing_price: exchange_rate.usd_value} | the_rest]) + + data -> + encode_market_history_data(data) end - |> encode_market_history_data() json(conn, %{ history_data: market_history_data, - supply_data: available_supply(Chain.supply_for_days(30), exchange_rate) + supply_data: available_supply(Chain.supply_for_days(), exchange_rate) }) else _ -> unprocessable_entity(conn) diff --git a/apps/block_scout_web/lib/block_scout_web/notifier.ex b/apps/block_scout_web/lib/block_scout_web/notifier.ex index 21a104848e..a5bc99e242 100644 --- a/apps/block_scout_web/lib/block_scout_web/notifier.ex +++ b/apps/block_scout_web/lib/block_scout_web/notifier.ex @@ -37,7 +37,7 @@ defmodule BlockScoutWeb.Notifier do exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() market_history_data = - case Market.fetch_recent_history(30) do + case Market.fetch_recent_history() do [today | the_rest] -> [%{today | closing_price: exchange_rate.usd_value} | the_rest] data -> data end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 9c203ad3e4..af02ce8274 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -52,6 +52,7 @@ defmodule Explorer.Chain do alias Explorer.Chain.Block.{EmissionReward, Reward} alias Explorer.Chain.Import.Runner alias Explorer.Counters.AddressesWithBalanceCounter + alias Explorer.Market.MarketHistoryCache alias Explorer.{PagingOptions, Repo} alias Dataloader.Ecto, as: DataloaderEcto @@ -2588,7 +2589,7 @@ defmodule Explorer.Chain do @doc """ Calls supply_for_days from the configured supply_module """ - def supply_for_days(days_count), do: supply_module().supply_for_days(days_count) + def supply_for_days, do: supply_module().supply_for_days(MarketHistoryCache.recent_days_count()) @doc """ Streams a lists token contract addresses that haven't been cataloged. diff --git a/apps/explorer/lib/explorer/market/market.ex b/apps/explorer/lib/explorer/market/market.ex index 0d1df671a2..f3e35f7d64 100644 --- a/apps/explorer/lib/explorer/market/market.ex +++ b/apps/explorer/lib/explorer/market/market.ex @@ -3,12 +3,10 @@ defmodule Explorer.Market do Context for data related to the cryptocurrency market. """ - import Ecto.Query - alias Explorer.Chain.Address.CurrentTokenBalance alias Explorer.Chain.Hash alias Explorer.ExchangeRates.Token - alias Explorer.Market.MarketHistory + alias Explorer.Market.{MarketHistory, MarketHistoryCache} alias Explorer.{ExchangeRates, KnownTokens, Repo} @doc """ @@ -35,18 +33,9 @@ defmodule Explorer.Market do Today's date is include as part of the day count """ - @spec fetch_recent_history(non_neg_integer()) :: [MarketHistory.t()] - def fetch_recent_history(days) when days >= 1 do - day_diff = days * -1 - - query = - from( - mh in MarketHistory, - where: mh.date > date_add(^Date.utc_today(), ^day_diff, "day"), - order_by: [desc: mh.date] - ) - - Repo.all(query) + @spec fetch_recent_history() :: [MarketHistory.t()] + def fetch_recent_history do + MarketHistoryCache.fetch() end @doc false diff --git a/apps/explorer/lib/explorer/market/market_history_cache.ex b/apps/explorer/lib/explorer/market/market_history_cache.ex index 404b11fb39..04b6193716 100644 --- a/apps/explorer/lib/explorer/market/market_history_cache.ex +++ b/apps/explorer/lib/explorer/market/market_history_cache.ex @@ -27,6 +27,10 @@ defmodule Explorer.Market.MarketHistoryCache do def data_key, do: @history_key + def updated_at_key, do: @last_update_key + + def recent_days_count, do: @recent_days + defp cache_expired? do updated_at = fetch_from_cache(@last_update_key) diff --git a/apps/explorer/test/explorer/market/market_history_cache_test.exs b/apps/explorer/test/explorer/market/market_history_cache_test.exs index 0b87aec6bc..04c49c64a3 100644 --- a/apps/explorer/test/explorer/market/market_history_cache_test.exs +++ b/apps/explorer/test/explorer/market/market_history_cache_test.exs @@ -4,6 +4,12 @@ defmodule Explorer.Market.MarketHistoryCacheTest do alias Explorer.Market alias Explorer.Market.MarketHistoryCache + setup do + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()}) + :ok + end + describe "fetch/1" do test "caches data on the first call" do today = Date.utc_today() @@ -25,6 +31,37 @@ defmodule Explorer.Market.MarketHistoryCacheTest do assert fetch_data() == records end + + test "updates cache if cache is stale" do + today = Date.utc_today() + + stale_records = + for i <- 0..29 do + %{ + date: Timex.shift(today, days: i * -1), + closing_price: Decimal.new(1), + opening_price: Decimal.new(1) + } + end + + Market.bulk_insert_history(stale_records) + + MarketHistoryCache.fetch() + + stale_updated_at = fetch_updated_at() + + assert fetch_data() == stale_records + + ConCache.put(MarketHistoryCache.cache_name(), MarketHistoryCache.updated_at_key(), Timex.shift(today, days: -35)) + + fetch_data() + + assert stale_updated_at != fetch_updated_at() + end + end + + defp fetch_updated_at do + ConCache.get(MarketHistoryCache.cache_name(), MarketHistoryCache.updated_at_key()) end defp fetch_data do