This reverts commit c5881cdc6d.
reapply-cache-change2
parent
4d7d88cfec
commit
317cb8aa85
@ -0,0 +1,107 @@
|
||||
defmodule Plausible.Cache.Adapter do
|
||||
@moduledoc """
|
||||
Interface for the underlying cache implementation.
|
||||
Currently: ConCache
|
||||
|
||||
Using the Adapter module directly, the user must ensure that the relevant
|
||||
processes are available to use, which is normally done via the child specification.
|
||||
"""
|
||||
|
||||
require Logger
|
||||
|
||||
@spec child_spec(atom(), atom(), Keyword.t()) :: Supervisor.child_spec()
|
||||
def child_spec(name, child_id, opts \\ [])
|
||||
when is_atom(name) and is_atom(child_id) and is_list(opts) do
|
||||
cache_name = Keyword.get(opts, :cache_name, name)
|
||||
child_id = Keyword.get(opts, :child_id, child_id)
|
||||
ttl_check_interval = Keyword.get(opts, :ttl_check_interval, false)
|
||||
|
||||
opts =
|
||||
opts
|
||||
|> Keyword.put(:name, cache_name)
|
||||
|> Keyword.put(:ttl_check_interval, ttl_check_interval)
|
||||
|
||||
Supervisor.child_spec(
|
||||
{ConCache, opts},
|
||||
id: child_id
|
||||
)
|
||||
end
|
||||
|
||||
@spec size(atom()) :: non_neg_integer() | nil
|
||||
def size(cache_name) do
|
||||
ConCache.size(cache_name)
|
||||
catch
|
||||
:exit, _ -> nil
|
||||
end
|
||||
|
||||
@spec get(atom(), any()) :: any()
|
||||
def get(cache_name, key) do
|
||||
cache_name
|
||||
|> ConCache.get(key)
|
||||
|> Plausible.Cache.Stats.track(cache_name)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec get(atom(), any(), (-> any())) :: any()
|
||||
def get(cache_name, key, fallback_fn) do
|
||||
cache_name
|
||||
|> ConCache.get_or_store(key, fn ->
|
||||
{:from_fallback, fallback_fn.()}
|
||||
end)
|
||||
|> Plausible.Cache.Stats.track(cache_name)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec put(atom(), any(), any()) :: any()
|
||||
def put(cache_name, key, value) do
|
||||
:ok = ConCache.put(cache_name, key, value)
|
||||
value
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error putting a key to '#{cache_name}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec put_many(atom(), [any()]) :: :ok
|
||||
def put_many(cache_name, items) when is_list(items) do
|
||||
true = :ets.insert(ConCache.ets(cache_name), items)
|
||||
:ok
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error putting keys to '#{cache_name}'")
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec delete(atom(), any()) :: :ok
|
||||
def delete(cache_name, key) do
|
||||
ConCache.dirty_delete(cache_name, key)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error deleting a key in '#{cache_name}'")
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec keys(atom()) :: Enumerable.t()
|
||||
def keys(cache_name) do
|
||||
ets = ConCache.ets(cache_name)
|
||||
|
||||
Stream.resource(
|
||||
fn -> :ets.first(ets) end,
|
||||
fn
|
||||
:"$end_of_table" -> {:halt, nil}
|
||||
prev_key -> {[prev_key], :ets.next(ets, prev_key)}
|
||||
end,
|
||||
fn _ -> :ok end
|
||||
)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
[]
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,74 @@
|
||||
defmodule Plausible.Cache.Stats do
|
||||
@moduledoc """
|
||||
Keeps track of hit/miss ratio for various caches.
|
||||
"""
|
||||
|
||||
use GenServer
|
||||
|
||||
def start_link(opts) do
|
||||
GenServer.start_link(__MODULE__, opts)
|
||||
end
|
||||
|
||||
def init(opts) do
|
||||
table = Keyword.get(opts, :table, __MODULE__)
|
||||
|
||||
^table =
|
||||
:ets.new(table, [
|
||||
:public,
|
||||
:named_table,
|
||||
:ordered_set,
|
||||
read_concurrency: true,
|
||||
write_concurrency: true
|
||||
])
|
||||
|
||||
{:ok, table}
|
||||
end
|
||||
|
||||
def gather(cache_name, table \\ __MODULE__) do
|
||||
{:ok,
|
||||
%{
|
||||
hit_rate: hit_rate(cache_name, table),
|
||||
count: size(cache_name) || 0
|
||||
}}
|
||||
end
|
||||
|
||||
defdelegate size(cache_name), to: Plausible.Cache.Adapter
|
||||
|
||||
def track(item, cache_name, table \\ __MODULE__)
|
||||
|
||||
def track({:from_fallback, item}, cache_name, table) do
|
||||
bump(cache_name, :miss, 1, table)
|
||||
item
|
||||
end
|
||||
|
||||
def track(nil, cache_name, table) do
|
||||
bump(cache_name, :miss, 1, table)
|
||||
nil
|
||||
end
|
||||
|
||||
def track(item, cache_name, table) do
|
||||
bump(cache_name, :hit, 1, table)
|
||||
item
|
||||
end
|
||||
|
||||
def bump(cache_name, type, increment, table \\ __MODULE__) do
|
||||
:ets.update_counter(
|
||||
table,
|
||||
{cache_name, type},
|
||||
increment,
|
||||
{{cache_name, type}, 0}
|
||||
)
|
||||
end
|
||||
|
||||
def hit_rate(cache_name, table \\ __MODULE__) do
|
||||
hit = :ets.lookup_element(table, {cache_name, :hit}, 2, 0)
|
||||
miss = :ets.lookup_element(table, {cache_name, :miss}, 2, 0)
|
||||
hit_miss = hit + miss
|
||||
|
||||
if hit_miss == 0 do
|
||||
0.0
|
||||
else
|
||||
hit / hit_miss * 100
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,50 @@
|
||||
defmodule Plausible.Cache.StatsTest do
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
alias Plausible.Cache.Stats
|
||||
|
||||
test "when tracking is not initialized, stats are 0" do
|
||||
assert {:ok, %{hit_rate: +0.0, count: 0}} = Stats.gather(:foo)
|
||||
end
|
||||
|
||||
test "when cache is started, stats are 0", %{test: test} do
|
||||
{:ok, _} = Stats.start_link(name: test, table: test)
|
||||
assert {:ok, %{hit_rate: +0.0, count: 0}} = Stats.gather(test, test)
|
||||
end
|
||||
|
||||
test "tracking changes hit ratio", %{test: test} do
|
||||
{:ok, _} = Stats.start_link(name: test, table: test)
|
||||
|
||||
Stats.track(nil, test, test)
|
||||
assert {:ok, %{hit_rate: +0.0, count: 0}} = Stats.gather(test, test)
|
||||
|
||||
Stats.track(:not_nil, test, test)
|
||||
assert {:ok, %{hit_rate: 50.0, count: 0}} = Stats.gather(test, test)
|
||||
|
||||
Stats.track(:not_nil, test, test)
|
||||
Stats.track(:not_nil, test, test)
|
||||
assert {:ok, %{hit_rate: 75.0, count: 0}} = Stats.gather(test, test)
|
||||
|
||||
Stats.track(nil, test, test)
|
||||
Stats.track(nil, test, test)
|
||||
assert {:ok, %{hit_rate: 50.0, count: 0}} = Stats.gather(test, test)
|
||||
end
|
||||
|
||||
test "bump by custom number", %{test: test} do
|
||||
{:ok, _} = Stats.start_link(name: test, table: test)
|
||||
Stats.bump(test, :miss, 10, test)
|
||||
assert {:ok, %{hit_rate: +0.0, count: 0}} = Stats.gather(test, test)
|
||||
Stats.bump(test, :hit, 90, test)
|
||||
assert {:ok, %{hit_rate: 90.0, count: 0}} = Stats.gather(test, test)
|
||||
end
|
||||
|
||||
test "count comes from cache adapter", %{test: test} do
|
||||
{:ok, _} = Stats.start_link(name: test, table: test)
|
||||
%{start: {m, f, a}} = Plausible.Cache.Adapter.child_spec(test, test)
|
||||
{:ok, _} = apply(m, f, a)
|
||||
|
||||
Plausible.Cache.Adapter.put(test, :key, :value)
|
||||
assert Stats.size(test) == 1
|
||||
assert {:ok, %{hit_rate: +0.0, count: 1}} = Stats.gather(test, test)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue