Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ttl properly in oredered caches #2628

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/explorer/lib/explorer/chain/ordered_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ defmodule Explorer.Chain.OrderedCache do
opts
|> Keyword.drop([:ids_list_key, :max_size, :preloads, :preload])
|> Keyword.put_new(:ttl_check_interval, false)
|> Keyword.put_new(:callback, &update_index_on_deletion(&1))

# credo:disable-for-next-line Credo.Check.Refactor.LongQuoteBlocks
quote do
Expand Down Expand Up @@ -301,6 +302,18 @@ defmodule Explorer.Chain.OrderedCache do
ConCache.dirty_put(cache_name(), element_id, full_element)
end

defp update_index_on_deletion({:delete, _cache_pid, key}) do
# Callback used so that the index list is updated in case of TTL usage.
# It check if the key is in the list first because ConCache's get is
# not locking and much faster, so it does not take much in case the
# callback is executed for an item that has already been removed.
if Enum.member?(ids_list(), id) do
ConCache.update(cache_name(), ids_list_key(), &List.delete(&1, id))
end
end

defp update_index_on_deletion(_data), do: nil

### Supervisor's child specification

@doc """
Expand Down