diff --git a/apps/explorer/lib/explorer/chain/block_number_cache.ex b/apps/explorer/lib/explorer/chain/block_number_cache.ex index d15d6781e6..b74e0999bf 100644 --- a/apps/explorer/lib/explorer/chain/block_number_cache.ex +++ b/apps/explorer/lib/explorer/chain/block_number_cache.ex @@ -6,13 +6,10 @@ defmodule Explorer.Chain.BlockNumberCache do alias Explorer.Chain @tab :block_number_cache - # 30 minutes - @cache_period 1_000 * 60 * 30 @key "min_max" - @opts_key "opts" - @spec setup(Keyword.t()) :: :ok - def setup(opts \\ []) do + @spec setup() :: :ok + def setup do if :ets.whereis(@tab) == :undefined do :ets.new(@tab, [ :set, @@ -22,7 +19,6 @@ defmodule Explorer.Chain.BlockNumberCache do ]) end - setup_opts(opts) update_cache() :ok @@ -41,16 +37,7 @@ defmodule Explorer.Chain.BlockNumberCache do end defp value(type) do - initial_cache = {_min, _max, old_current_time} = cached_values() - - {min, max, _current_time} = - if current_time() - old_current_time > cache_period() do - update_cache() - - cached_values() - else - initial_cache - end + {min, max} = cached_values() case type do :max -> max @@ -61,15 +48,15 @@ defmodule Explorer.Chain.BlockNumberCache do @spec update(non_neg_integer()) :: boolean() def update(number) do - {old_min, old_max, time} = cached_values() + {old_min, old_max} = cached_values() cond do number > old_max -> - tuple = {old_min, number, time} + tuple = {old_min, number} :ets.insert(@tab, {@key, tuple}) number < old_min -> - tuple = {number, old_max, time} + tuple = {number, old_max} :ets.insert(@tab, {@key, tuple}) true -> @@ -78,41 +65,22 @@ defmodule Explorer.Chain.BlockNumberCache do end defp update_cache do - current_time = current_time() {min, max} = min_and_max_from_db() - tuple = {min, max, current_time} + tuple = {min, max} :ets.insert(@tab, {@key, tuple}) end - defp setup_opts(opts) do - cache_period = opts[:cache_period] || @cache_period - - :ets.insert(@tab, {@opts_key, cache_period}) - end - defp cached_values do [{_, cached_values}] = :ets.lookup(@tab, @key) cached_values end - defp cache_period do - [{_, cache_period}] = :ets.lookup(@tab, @opts_key) - - cache_period - end - defp min_and_max_from_db do Chain.fetch_min_and_max_block_numbers() rescue _e -> {0, 0} end - - defp current_time do - utc_now = DateTime.utc_now() - - DateTime.to_unix(utc_now, :millisecond) - end end diff --git a/apps/explorer/test/explorer/chain/block_number_cache_test.exs b/apps/explorer/test/explorer/chain/block_number_cache_test.exs index a33263293c..870b1b4240 100644 --- a/apps/explorer/test/explorer/chain/block_number_cache_test.exs +++ b/apps/explorer/test/explorer/chain/block_number_cache_test.exs @@ -11,33 +11,6 @@ defmodule Explorer.Chain.BlockNumberCacheTest do assert BlockNumberCache.max_number() == 5 end - - test "invalidates cache if period did pass" do - insert(:block, number: 5) - - BlockNumberCache.setup(cache_period: 2_000) - - assert BlockNumberCache.max_number() == 5 - - insert(:block, number: 10) - - Process.sleep(2_000) - - assert BlockNumberCache.max_number() == 10 - assert BlockNumberCache.min_number() == 5 - end - - test "does not invalidate cache if period time did not pass" do - insert(:block, number: 5) - - BlockNumberCache.setup(cache_period: 10_000) - - assert BlockNumberCache.max_number() == 5 - - insert(:block, number: 10) - - assert BlockNumberCache.max_number() == 5 - end end describe "min_number/1" do @@ -48,32 +21,31 @@ defmodule Explorer.Chain.BlockNumberCacheTest do assert BlockNumberCache.max_number() == 2 end + end - test "invalidates cache" do - insert(:block, number: 5) - - BlockNumberCache.setup(cache_period: 2_000) + describe "update/1" do + test "updates max number" do + insert(:block, number: 2) - assert BlockNumberCache.min_number() == 5 + BlockNumberCache.setup() - insert(:block, number: 2) + assert BlockNumberCache.max_number() == 2 - Process.sleep(2_000) + assert BlockNumberCache.update(3) - assert BlockNumberCache.min_number() == 2 - assert BlockNumberCache.max_number() == 5 + assert BlockNumberCache.max_number() == 3 end - test "does not invalidate cache if period time did not pass" do - insert(:block, number: 5) + test "updates min number" do + insert(:block, number: 2) - BlockNumberCache.setup(cache_period: 10_000) + BlockNumberCache.setup() - assert BlockNumberCache.max_number() == 5 + assert BlockNumberCache.min_number() == 2 - insert(:block, number: 2) + assert BlockNumberCache.update(1) - assert BlockNumberCache.max_number() == 5 + assert BlockNumberCache.min_number() == 1 end end end diff --git a/apps/explorer/test/support/data_case.ex b/apps/explorer/test/support/data_case.ex index 75d3429e70..68bb21434c 100644 --- a/apps/explorer/test/support/data_case.ex +++ b/apps/explorer/test/support/data_case.ex @@ -39,7 +39,7 @@ defmodule Explorer.DataCase do Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()}) end - Explorer.Chain.BlockNumberCache.setup(cache_period: 0) + Explorer.Chain.BlockNumberCache.setup() :ok end