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. 23
      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,
BlockCountCache,
BlockNumberCache,
BlocksCache,
Data,
DecompiledSmartContract,
Hash,
@ -1148,16 +1149,22 @@ defmodule Explorer.Chain do
@spec list_blocks([paging_options | necessity_by_association_option]) :: [Block.t()]
def list_blocks(options \\ []) when is_list(options) do
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
|> Block.block_type_filter(block_type)
|> page_blocks(paging_options)
|> limit(^paging_options.page_size)
|> order_by(desc: :number)
|> join_associations(necessity_by_association)
|> Repo.all()
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_type_filter(block_type)
|> page_blocks(paging_options)
|> limit(^paging_options.page_size)
|> order_by(desc: :number)
|> join_associations(necessity_by_association)
|> Repo.all()
end
end
@doc """

@ -22,6 +22,16 @@ defmodule Explorer.Chain.BlocksCache do
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
numbers = block_numbers()

@ -11,7 +11,7 @@ defmodule Indexer.Block.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries}
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.Fetcher.{
@ -186,6 +186,7 @@ defmodule Indexer.Block.Fetcher do
BlockNumberCache.update(max_block.number)
BlockNumberCache.update(min_block.number)
BlocksCache.update_blocks(blocks)
end
def import(

Loading…
Cancel
Save