diff options
author | William Carroll <wpcarro@gmail.com> | 2020-12-12T01·04+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-12-12T01·04+0000 |
commit | 714ec29743bd90257ed7607635abe885df01267d (patch) | |
tree | f89609c53248ff43f8d336c0afa33f24aa0e613c | |
parent | c0a88ba7d5524fdaca5d32241676b8bb26e6c3eb (diff) |
Define Cache and convert app to OTP
Define a simple in-memory key-value store for our cache. TL;DR: - Define `Cache` as a simple state-keeping `Agent` - Define `Sup`, which starts our Cache process - Define `App`, which starts our Supervisor process - Whitelist `App` in `mix.exs`, so that it starts after calling `iex -S mix`
-rw-r--r-- | assessments/semiprimes/server/lib/app.ex | 8 | ||||
-rw-r--r-- | assessments/semiprimes/server/lib/cache.ex | 27 | ||||
-rw-r--r-- | assessments/semiprimes/server/lib/sup.ex | 21 | ||||
-rw-r--r-- | assessments/semiprimes/server/mix.exs | 3 |
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 |