From 85439d87400ea5713d21346502f99bfddad6adb0 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 7 Jun 2019 11:13:38 +0300 Subject: [PATCH] optimize blocks fetching from cache --- apps/explorer/lib/explorer/chain.ex | 4 +--- apps/explorer/lib/explorer/chain/blocks_cache.ex | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 0c91ef33db..4edc94887a 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1155,9 +1155,7 @@ defmodule Explorer.Chain do if block_type == "Block" && !paging_options.key do if BlocksCache.enough_elements?(paging_options.page_size) do - cached_blocks = BlocksCache.blocks() - - Enum.slice(cached_blocks, 0, paging_options.page_size) + BlocksCache.blocks(paging_options.page_size) else elements = fetch_blocks(block_type, paging_options, necessity_by_association) diff --git a/apps/explorer/lib/explorer/chain/blocks_cache.ex b/apps/explorer/lib/explorer/chain/blocks_cache.ex index 3b9174d965..778d2bc5d6 100644 --- a/apps/explorer/lib/explorer/chain/blocks_cache.ex +++ b/apps/explorer/lib/explorer/chain/blocks_cache.ex @@ -56,12 +56,15 @@ defmodule Explorer.Chain.BlocksCache do end) end - def blocks do + def blocks(number \\ nil) do numbers = block_numbers() + number = if is_nil(number), do: Enum.count(numbers), else: number + numbers |> Enum.sort() |> Enum.reverse() + |> Enum.slice(0, number) |> Enum.map(fn number -> ConCache.get(@cache_name, number) end)