use cached data

pull/2182/head
Ayrat Badykov 6 years ago
parent e3f2a64ccd
commit 4fd053bc39
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 16
      apps/block_scout_web/lib/block_scout_web/controllers/chain/market_history_chart_controller.ex
  2. 2
      apps/block_scout_web/lib/block_scout_web/notifier.ex
  3. 3
      apps/explorer/lib/explorer/chain.ex
  4. 19
      apps/explorer/lib/explorer/market/market.ex
  5. 4
      apps/explorer/lib/explorer/market/market_history_cache.ex
  6. 37
      apps/explorer/test/explorer/market/market_history_cache_test.exs

@ -8,18 +8,20 @@ defmodule BlockScoutWeb.Chain.MarketHistoryChartController do
with true <- ajax?(conn) do with true <- ajax?(conn) do
exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null()
recent_market_history = Market.fetch_recent_history()
market_history_data = market_history_data =
30 case recent_market_history do
|> Market.fetch_recent_history() [today | the_rest] ->
|> case do encode_market_history_data([%{today | closing_price: exchange_rate.usd_value} | the_rest])
[today | the_rest] -> [%{today | closing_price: exchange_rate.usd_value} | the_rest]
data -> data data ->
encode_market_history_data(data)
end end
|> encode_market_history_data()
json(conn, %{ json(conn, %{
history_data: market_history_data, 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 else
_ -> unprocessable_entity(conn) _ -> unprocessable_entity(conn)

@ -37,7 +37,7 @@ defmodule BlockScoutWeb.Notifier do
exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null()
market_history_data = 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] [today | the_rest] -> [%{today | closing_price: exchange_rate.usd_value} | the_rest]
data -> data data -> data
end end

@ -52,6 +52,7 @@ defmodule Explorer.Chain do
alias Explorer.Chain.Block.{EmissionReward, Reward} alias Explorer.Chain.Block.{EmissionReward, Reward}
alias Explorer.Chain.Import.Runner alias Explorer.Chain.Import.Runner
alias Explorer.Counters.AddressesWithBalanceCounter alias Explorer.Counters.AddressesWithBalanceCounter
alias Explorer.Market.MarketHistoryCache
alias Explorer.{PagingOptions, Repo} alias Explorer.{PagingOptions, Repo}
alias Dataloader.Ecto, as: DataloaderEcto alias Dataloader.Ecto, as: DataloaderEcto
@ -2588,7 +2589,7 @@ defmodule Explorer.Chain do
@doc """ @doc """
Calls supply_for_days from the configured supply_module 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 """ @doc """
Streams a lists token contract addresses that haven't been cataloged. Streams a lists token contract addresses that haven't been cataloged.

@ -3,12 +3,10 @@ defmodule Explorer.Market do
Context for data related to the cryptocurrency market. Context for data related to the cryptocurrency market.
""" """
import Ecto.Query
alias Explorer.Chain.Address.CurrentTokenBalance alias Explorer.Chain.Address.CurrentTokenBalance
alias Explorer.Chain.Hash alias Explorer.Chain.Hash
alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Token
alias Explorer.Market.MarketHistory alias Explorer.Market.{MarketHistory, MarketHistoryCache}
alias Explorer.{ExchangeRates, KnownTokens, Repo} alias Explorer.{ExchangeRates, KnownTokens, Repo}
@doc """ @doc """
@ -35,18 +33,9 @@ defmodule Explorer.Market do
Today's date is include as part of the day count Today's date is include as part of the day count
""" """
@spec fetch_recent_history(non_neg_integer()) :: [MarketHistory.t()] @spec fetch_recent_history() :: [MarketHistory.t()]
def fetch_recent_history(days) when days >= 1 do def fetch_recent_history do
day_diff = days * -1 MarketHistoryCache.fetch()
query =
from(
mh in MarketHistory,
where: mh.date > date_add(^Date.utc_today(), ^day_diff, "day"),
order_by: [desc: mh.date]
)
Repo.all(query)
end end
@doc false @doc false

@ -27,6 +27,10 @@ defmodule Explorer.Market.MarketHistoryCache do
def data_key, do: @history_key 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 defp cache_expired? do
updated_at = fetch_from_cache(@last_update_key) updated_at = fetch_from_cache(@last_update_key)

@ -4,6 +4,12 @@ defmodule Explorer.Market.MarketHistoryCacheTest do
alias Explorer.Market alias Explorer.Market
alias Explorer.Market.MarketHistoryCache 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 describe "fetch/1" do
test "caches data on the first call" do test "caches data on the first call" do
today = Date.utc_today() today = Date.utc_today()
@ -25,6 +31,37 @@ defmodule Explorer.Market.MarketHistoryCacheTest do
assert fetch_data() == records assert fetch_data() == records
end 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 end
defp fetch_data do defp fetch_data do

Loading…
Cancel
Save