about summary refs log tree commit diff
path: root/assessments/semiprimes/server
diff options
context:
space:
mode:
Diffstat (limited to 'assessments/semiprimes/server')
-rw-r--r--assessments/semiprimes/server/lib/app.ex8
-rw-r--r--assessments/semiprimes/server/lib/cache.ex27
-rw-r--r--assessments/semiprimes/server/lib/sup.ex21
-rw-r--r--assessments/semiprimes/server/mix.exs3
4 files changed, 58 insertions, 1 deletions
diff --git a/assessments/semiprimes/server/lib/app.ex b/assessments/semiprimes/server/lib/app.ex
new file mode 100644
index 000000000000..7a6fa5ea248d
--- /dev/null
+++ b/assessments/semiprimes/server/lib/app.ex
@@ -0,0 +1,8 @@
+defmodule App do
+  use Application
+
+  @impl true
+  def start(_type, _args) do
+    Sup.start_link()
+  end
+end
diff --git a/assessments/semiprimes/server/lib/cache.ex b/assessments/semiprimes/server/lib/cache.ex
new file mode 100644
index 000000000000..334bf901b6e6
--- /dev/null
+++ b/assessments/semiprimes/server/lib/cache.ex
@@ -0,0 +1,27 @@
+defmodule Cache do
+  @moduledoc """
+  Cache is an in-memory key-value store.
+  """
+  use Agent
+
+  @doc """
+  Inititalize the key-value store.
+  """
+  def start_link(_) do
+    Agent.start_link(fn -> %{} end, name: __MODULE__)
+  end
+
+  @doc """
+  Attempt to return the value stored at `key`
+  """
+  def get(key) do
+    Agent.get(__MODULE__, &Map.get(&1, key))
+  end
+
+  @doc """
+  Write the `value` under the `key`. Last writer wins.
+  """
+  def put(key, value) do
+    Agent.update(__MODULE__, &Map.put(&1, key, value))
+  end
+end
diff --git a/assessments/semiprimes/server/lib/sup.ex b/assessments/semiprimes/server/lib/sup.ex
new file mode 100644
index 000000000000..ea02ce30e7b9
--- /dev/null
+++ b/assessments/semiprimes/server/lib/sup.ex
@@ -0,0 +1,21 @@
+defmodule Sup do
+  @moduledoc """
+  Top-level supervisor for our OTP application. For now, this supervisor starts
+  and monitors our cache.
+  """
+
+  use Supervisor
+
+  def start_link(opts \\ []) do
+    Supervisor.start_link(__MODULE__, :ok, opts)
+  end
+
+  @impl true
+  def init(:ok) do
+    children = [
+      Cache
+    ]
+
+    Supervisor.init(children, strategy: :one_for_one)
+  end
+end
diff --git a/assessments/semiprimes/server/mix.exs b/assessments/semiprimes/server/mix.exs
index 0efb638e1728..2bbd357c3854 100644
--- a/assessments/semiprimes/server/mix.exs
+++ b/assessments/semiprimes/server/mix.exs
@@ -14,7 +14,8 @@ defmodule Server.MixProject do
   # Run "mix help compile.app" to learn about applications.
   def application do
     [
-      extra_applications: [:logger]
+      extra_applications: [:logger],
+      mod: {App, []}
     ]
   end