From 73e4c917d0eea9debc29bd6a5e08823a05caf84f Mon Sep 17 00:00:00 2001 From: DualFroz Date: Thu, 27 Aug 2026 20:23:00 +0200 Subject: [PATCH] feat(app): add otp application and public api --- lib/excache.ex | 9 +++++++++ lib/excache/application.ex | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 lib/excache.ex create mode 100644 lib/excache/application.ex diff --git a/lib/excache.ex b/lib/excache.ex new file mode 100644 index 0000000..68f1511 --- /dev/null +++ b/lib/excache.ex @@ -0,0 +1,9 @@ +defmodule Excache do + @moduledoc """ + In-memory key-value server speaking a subset of the RESP2 protocol. + """ + + defdelegate save(), to: Excache.Persistence + defdelegate flushdb(), to: Excache.Store + defdelegate dbsize(), to: Excache.Store +end diff --git a/lib/excache/application.ex b/lib/excache/application.ex new file mode 100644 index 0000000..58e3792 --- /dev/null +++ b/lib/excache/application.ex @@ -0,0 +1,36 @@ +defmodule Excache.Application do + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + port = Application.get_env(:excache, :port, 6379) + + children = [ + {Registry, keys: :duplicate, name: Excache.PubSub.Registry}, + Excache.Store.Supervisor, + Excache.Aof, + Excache.Expiry, + {DynamicSupervisor, strategy: :one_for_one, name: Excache.Server.ConnectionSupervisor} + ] + + case Supervisor.start_link(children, strategy: :one_for_one, name: Excache.Supervisor) do + {:ok, supervisor} -> + Excache.Aof.replay(Excache.Aof.path()) + start_acceptor(supervisor, port) + + error -> + error + end + end + + defp start_acceptor(supervisor, port) when is_integer(port) do + case Supervisor.start_child(supervisor, {Excache.Server.Acceptor, [port: port]}) do + {:ok, _pid} -> {:ok, supervisor} + error -> error + end + end + + defp start_acceptor(supervisor, _port), do: {:ok, supervisor} +end