feat: Set dynamic ttl of cache modules derived from MapCache (#10109)

* Set dynamic ttl of cache modules derived from MapCache

* Update apps/explorer/lib/explorer/chain/cache/helper.ex

Co-authored-by: Qwerty5Uiop <105209995+Qwerty5Uiop@users.noreply.github.com>

---------

Co-authored-by: Qwerty5Uiop <105209995+Qwerty5Uiop@users.noreply.github.com>
pull/10203/head
Victor Baranov 6 months ago committed by GitHub
parent 80a8e3b464
commit 44bee1ef0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      apps/explorer/lib/explorer/chain/cache/address_sum.ex
  2. 5
      apps/explorer/lib/explorer/chain/cache/address_sum_minus_burnt.ex
  3. 4
      apps/explorer/lib/explorer/chain/cache/block.ex
  4. 5
      apps/explorer/lib/explorer/chain/cache/gas_usage.ex
  5. 33
      apps/explorer/lib/explorer/chain/cache/helper.ex
  6. 4
      apps/explorer/lib/explorer/chain/cache/transaction.ex

@ -10,9 +10,10 @@ defmodule Explorer.Chain.Cache.AddressSum do
key: :sum,
key: :async_task,
ttl_check_interval: Application.get_env(:explorer, __MODULE__)[:ttl_check_interval],
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl],
global_ttl: :infinity,
callback: &async_task_on_deletion(&1)
alias Explorer.Chain.Cache.Helper
alias Explorer.Etherscan
defp handle_fallback(:sum) do
@ -31,7 +32,7 @@ defmodule Explorer.Chain.Cache.AddressSum do
try do
result = Etherscan.fetch_sum_coin_total_supply()
set_sum(result)
set_sum(%ConCache.Item{ttl: Helper.ttl(__MODULE__, "CACHE_ADDRESS_SUM_PERIOD"), value: result})
rescue
e ->
Logger.debug([

@ -10,10 +10,11 @@ defmodule Explorer.Chain.Cache.AddressSumMinusBurnt do
key: :sum_minus_burnt,
key: :async_task,
ttl_check_interval: Application.get_env(:explorer, __MODULE__)[:ttl_check_interval],
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl],
global_ttl: :infinity,
callback: &async_task_on_deletion(&1)
alias Explorer.{Chain, Etherscan}
alias Explorer.Chain.Cache.Helper
defp handle_fallback(:sum_minus_burnt) do
# This will get the task PID if one exists and launch a new task if not
@ -38,7 +39,7 @@ defmodule Explorer.Chain.Cache.AddressSumMinusBurnt do
Chain.upsert_last_fetched_counter(params)
set_sum_minus_burnt(result)
set_sum_minus_burnt(%ConCache.Item{ttl: Helper.ttl(__MODULE__, "CACHE_ADDRESS_SUM_PERIOD"), value: result})
rescue
e ->
Logger.debug([

@ -12,7 +12,7 @@ defmodule Explorer.Chain.Cache.Block do
name: :block_count,
key: :count,
key: :async_task,
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl],
global_ttl: :infinity,
ttl_check_interval: :timer.seconds(1),
callback: &async_task_on_deletion(&1)
@ -78,7 +78,7 @@ defmodule Explorer.Chain.Cache.Block do
Chain.upsert_last_fetched_counter(params)
set_count(result)
set_count(%ConCache.Item{ttl: Helper.ttl(__MODULE__, "CACHE_BLOCK_COUNT_PERIOD"), value: result})
rescue
e ->
Logger.debug([

@ -17,10 +17,11 @@ defmodule Explorer.Chain.Cache.GasUsage do
name: :gas_usage,
key: :sum,
key: :async_task,
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl],
global_ttl: :infinity,
ttl_check_interval: :timer.seconds(1),
callback: &async_task_on_deletion(&1)
alias Explorer.Chain.Cache.Helper
alias Explorer.Chain.Transaction
alias Explorer.Repo
@ -52,7 +53,7 @@ defmodule Explorer.Chain.Cache.GasUsage do
try do
result = fetch_sum_gas_used()
set_sum(result)
set_sum(%ConCache.Item{ttl: Helper.ttl(__MODULE__, "CACHE_TOTAL_GAS_USAGE_PERIOD"), value: result})
rescue
e ->
Logger.debug([

@ -4,6 +4,10 @@ defmodule Explorer.Chain.Cache.Helper do
"""
alias Explorer.Chain
@block_number_threshold_1 10_000
@block_number_threshold_2 50_000
@block_number_threshold_3 150_000
@doc """
Estimates the row count of a given table using PostgreSQL system catalogs.
@ -28,4 +32,33 @@ defmodule Explorer.Chain.Cache.Helper do
count
end
@doc """
Calculates the time-to-live (TTL) for a given module in the cache.
## Parameters
* `module` - The module for which to calculate the TTL.
* `management_variable` - The management environment variable.
## Returns
The TTL for the module.
"""
@spec ttl(atom, String.t()) :: non_neg_integer()
def ttl(module, management_variable) do
min_blockchain_block_number = Application.get_env(:indexer, :first_block)
max_block_number = Chain.fetch_max_block_number()
blocks_amount = max_block_number - min_blockchain_block_number
global_ttl_from_var = Application.get_env(:explorer, module)[:global_ttl]
cond do
System.get_env(management_variable) not in ["", nil] -> global_ttl_from_var
blocks_amount < @block_number_threshold_1 -> :timer.seconds(10)
blocks_amount >= @block_number_threshold_1 and blocks_amount < @block_number_threshold_2 -> :timer.seconds(30)
blocks_amount >= @block_number_threshold_2 and blocks_amount < @block_number_threshold_3 -> :timer.minutes(2)
true -> global_ttl_from_var
end
end
end

@ -7,7 +7,7 @@ defmodule Explorer.Chain.Cache.Transaction do
name: :transaction_count,
key: :count,
key: :async_task,
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl],
global_ttl: :infinity,
ttl_check_interval: :timer.seconds(1),
callback: &async_task_on_deletion(&1)
@ -51,7 +51,7 @@ defmodule Explorer.Chain.Cache.Transaction do
try do
result = Repo.aggregate(Transaction, :count, :hash, timeout: :infinity)
set_count(result)
set_count(%ConCache.Item{ttl: Helper.ttl(__MODULE__, "CACHE_TXS_COUNT_PERIOD"), value: result})
rescue
e ->
Logger.debug([

Loading…
Cancel
Save