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 /assessments/semiprimes/server/lib/cache.ex | |
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`
Diffstat (limited to 'assessments/semiprimes/server/lib/cache.ex')
-rw-r--r-- | assessments/semiprimes/server/lib/cache.ex | 27 |
1 files changed, 27 insertions, 0 deletions
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 |