Created
April 29, 2018 23:29
-
-
Save micahboyd/8dd2e2d5bd9c4f28209536ef46da2e62 to your computer and use it in GitHub Desktop.
Elixir GenServer practice 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule KeyValueStore do | |
| use GenServer | |
| def start, do: GenServer.start(KeyValueStore, nil) | |
| def put(pid, key, value) do | |
| GenServer.cast(pid, {:put, key, value}) | |
| end | |
| def get(pid, key) do | |
| GenServer.call(pid, {:get, key}) | |
| end | |
| def init(_), do: {:ok, %{}} | |
| def handle_cast({:put, key, value}, state) do | |
| {:no_reply, Map.put(state, key, value)} | |
| end | |
| def handle_call({:get, key}, _, state) do | |
| {:reply, Map.get(state, key), state} | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment