chore: Refactor OrderedCache preloads (#10803)

pull/10814/head
Qwerty5Uiop 2 months ago committed by GitHub
parent b80a27ca28
commit fa00acfb74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      apps/explorer/lib/explorer/chain/ordered_cache.ex

@ -247,6 +247,7 @@ defmodule Explorer.Chain.OrderedCache do
ConCache.update(cache_name(), ids_list_key(), fn ids ->
updated_list =
elements
|> do_preloads()
|> Enum.map(&{element_to_id(&1), &1})
|> Enum.sort(&prevails?(&1, &2))
|> merge_and_update(ids || [], max_size())
@ -258,6 +259,14 @@ defmodule Explorer.Chain.OrderedCache do
def update(element), do: update([element])
defp do_preloads(elements) do
if Enum.empty?(preloads()) do
elements
else
Explorer.Repo.preload(elements, preloads())
end
end
defp merge_and_update(_candidates, existing, 0) do
# if there is no more space in the list remove the remaining existing
# elements and return an empty list
@ -323,17 +332,10 @@ defmodule Explorer.Chain.OrderedCache do
end
defp put_element(element_id, element) do
full_element =
if Enum.empty?(preloads()) do
element
else
Explorer.Repo.preload(element, preloads())
end
# dirty puts are a little faster than puts with locks.
# this is not a problem because this is the only function modifying rows
# and it only gets called inside `update`, which works isolated
ConCache.dirty_put(cache_name(), element_id, full_element)
ConCache.dirty_put(cache_name(), element_id, element)
end
### Supervisor's child specification

Loading…
Cancel
Save