|
|
|
@ -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 |
|
|
|
|