defmodule Vela.Topology.Local do @moduledoc """ Single-node topology. All data lives on the local node. Delegates directly to the configured backend. """ @behaviour Vela.Topology alias Vela.Cache.Entry # State holds the backend module and its state defstruct [:backend, :backend_state] @impl true def init(config) do case config.backend.init(config) do {:ok, backend_state} -> {:ok, %__MODULE__{ backend: config.backend, backend_state: backend_state }} error -> error end end @impl true def get(%__MODULE__{backend: b, backend_state: bs}, key) do b.get(bs, key) end @impl true def put(%__MODULE__{backend: b, backend_state: bs} = state, %Entry{} = entry) do case b.put(bs, entry) do {:ok, new_bs} -> {:ok, %{state | backend_state: new_bs}} error -> error end end @impl true def delete(%__MODULE__{backend: b, backend_state: bs} = state, key) do {:ok, new_bs} = b.delete(bs, key) {:ok, %{state | backend_state: new_bs}} end @impl true def get_many(%__MODULE__{backend: b, backend_state: bs}, keys) do b.get_many(bs, keys) end @impl true def put_many(%__MODULE__{backend: b, backend_state: bs} = state, entries) do case b.put_many(bs, entries) do {:ok, new_bs} -> {:ok, %{state | backend_state: new_bs}} error -> error end end @impl true def flush(%__MODULE__{backend: b, backend_state: bs} = state) do {:ok, new_bs} = b.flush(bs) {:ok, %{state | backend_state: new_bs}} end @impl true def size(%__MODULE__{backend: b, backend_state: bs}) do b.size(bs) end @impl true def invalidate_tag(%__MODULE__{backend: b, backend_state: bs} = state, tag) do {:ok, count, new_bs} = b.delete_by_tag(bs, tag) {:ok, count, %{state | backend_state: new_bs}} end end