load blocks from cache in controller

pull/2075/head
Ayrat Badykov 6 years ago
parent 5d8c69da80
commit 4011865ec9
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 9
      apps/explorer/lib/explorer/chain.ex
  2. 10
      apps/explorer/lib/explorer/chain/blocks_cache.ex
  3. 3
      apps/indexer/lib/indexer/block/fetcher.ex

@ -32,6 +32,7 @@ defmodule Explorer.Chain do
Block, Block,
BlockCountCache, BlockCountCache,
BlockNumberCache, BlockNumberCache,
BlocksCache,
Data, Data,
DecompiledSmartContract, DecompiledSmartContract,
Hash, Hash,
@ -1148,9 +1149,14 @@ defmodule Explorer.Chain do
@spec list_blocks([paging_options | necessity_by_association_option]) :: [Block.t()] @spec list_blocks([paging_options | necessity_by_association_option]) :: [Block.t()]
def list_blocks(options \\ []) when is_list(options) do def list_blocks(options \\ []) when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, @default_paging_options) paging_options = Keyword.get(options, :paging_options) || @default_paging_options
block_type = Keyword.get(options, :block_type, "Block") block_type = Keyword.get(options, :block_type, "Block")
if block_type == "Block" && !paging_options.key && BlocksCache.enough_elements?(paging_options.page_size) do
cached_blocks = BlocksCache.blocks()
Enum.slice(cached_blocks, 0, paging_options.page_size)
else
Block Block
|> Block.block_type_filter(block_type) |> Block.block_type_filter(block_type)
|> page_blocks(paging_options) |> page_blocks(paging_options)
@ -1159,6 +1165,7 @@ defmodule Explorer.Chain do
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()
end end
end
@doc """ @doc """
Map `block_number`s to their `t:Explorer.Chain.Block.t/0` `hash` `t:Explorer.Chain.Hash.Full.t/0`. Map `block_number`s to their `t:Explorer.Chain.Block.t/0` `hash` `t:Explorer.Chain.Hash.Full.t/0`.

@ -22,6 +22,16 @@ defmodule Explorer.Chain.BlocksCache do
end end
end end
def enough_elements?(number) do
ConCache.size(@cache_name) > number + 1
end
def update_blocks(blocks) do
Enum.each(blocks, fn block ->
update(block)
end)
end
def blocks do def blocks do
numbers = block_numbers() numbers = block_numbers()

@ -11,7 +11,7 @@ defmodule Indexer.Block.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries} alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries}
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.{Address, Block, BlockNumberCache, Hash, Import, Transaction} alias Explorer.Chain.{Address, Block, BlockNumberCache, BlocksCache, Hash, Import, Transaction}
alias Indexer.Block.Fetcher.Receipts alias Indexer.Block.Fetcher.Receipts
alias Indexer.Fetcher.{ alias Indexer.Fetcher.{
@ -186,6 +186,7 @@ defmodule Indexer.Block.Fetcher do
BlockNumberCache.update(max_block.number) BlockNumberCache.update(max_block.number)
BlockNumberCache.update(min_block.number) BlockNumberCache.update(min_block.number)
BlocksCache.update_blocks(blocks)
end end
def import( def import(

Loading…
Cancel
Save