From a7f94afb1a72979383ab7b58eae89d47b4bb6644 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 6 Oct 2020 10:43:11 +0300 Subject: [PATCH] Fix last_update cache key to store txs count --- .../controllers/address_controller_test.exs | 2 +- .../counters/address_transactions_counter.ex | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/address_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/address_controller_test.exs index d68f67f25c..cf0126937d 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/address_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/address_controller_test.exs @@ -85,7 +85,7 @@ defmodule BlockScoutWeb.AddressControllerTest do assert conn.status == 200 {:ok, response} = Jason.decode(conn.resp_body) - assert %{"transaction_count" => nil, "validation_count" => 0} == response + assert %{"transaction_count" => 0, "validation_count" => 0} == response end end end diff --git a/apps/explorer/lib/explorer/counters/address_transactions_counter.ex b/apps/explorer/lib/explorer/counters/address_transactions_counter.ex index 07ecc5e16b..edcdf3de5b 100644 --- a/apps/explorer/lib/explorer/counters/address_transactions_counter.ex +++ b/apps/explorer/lib/explorer/counters/address_transactions_counter.ex @@ -48,7 +48,7 @@ defmodule Explorer.Counters.AddressTransactionsCounter do end def fetch(address) do - if cache_expired?() do + if cache_expired?(address) do Task.start_link(fn -> update_cache(address) end) @@ -60,10 +60,9 @@ defmodule Explorer.Counters.AddressTransactionsCounter do def cache_name, do: @cache_name - def updated_at_key, do: @last_update_key - - defp cache_expired? do - updated_at = fetch_from_cache(@last_update_key) + defp cache_expired?(address) do + address_hash_string = get_address_hash_string(address) + updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") cond do is_nil(updated_at) -> true @@ -75,7 +74,7 @@ defmodule Explorer.Counters.AddressTransactionsCounter do defp update_cache(address) do new_data = Chain.address_to_transaction_count(address) address_hash_string = get_address_hash_string(address) - put_into_cache(@last_update_key, current_time()) + put_into_cache("hash_#{address_hash_string}_#{@last_update_key}", current_time()) put_into_cache("hash_#{address_hash_string}", new_data) end @@ -85,7 +84,7 @@ defmodule Explorer.Counters.AddressTransactionsCounter do value [] -> - nil + 0 end end