optimize blocks fetching from cache

pull/2075/head
Ayrat Badykov 6 years ago
parent b40f2e8c5a
commit 85439d8740
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 4
      apps/explorer/lib/explorer/chain.ex
  2. 5
      apps/explorer/lib/explorer/chain/blocks_cache.ex

@ -1155,9 +1155,7 @@ defmodule Explorer.Chain do
if block_type == "Block" && !paging_options.key do if block_type == "Block" && !paging_options.key do
if BlocksCache.enough_elements?(paging_options.page_size) do if BlocksCache.enough_elements?(paging_options.page_size) do
cached_blocks = BlocksCache.blocks() BlocksCache.blocks(paging_options.page_size)
Enum.slice(cached_blocks, 0, paging_options.page_size)
else else
elements = fetch_blocks(block_type, paging_options, necessity_by_association) elements = fetch_blocks(block_type, paging_options, necessity_by_association)

@ -56,12 +56,15 @@ defmodule Explorer.Chain.BlocksCache do
end) end)
end end
def blocks do def blocks(number \\ nil) do
numbers = block_numbers() numbers = block_numbers()
number = if is_nil(number), do: Enum.count(numbers), else: number
numbers numbers
|> Enum.sort() |> Enum.sort()
|> Enum.reverse() |> Enum.reverse()
|> Enum.slice(0, number)
|> Enum.map(fn number -> |> Enum.map(fn number ->
ConCache.get(@cache_name, number) ConCache.get(@cache_name, number)
end) end)

Loading…
Cancel
Save