Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # handle_info, handles
- # GenServer.call(pid, {:something, :key})
- Notice that you didn’t use match-all clauses for handle_cast and handle_call in previous examples.
- This was intentional. Casts and calls are well-defined requests.
- They specify an interface between clients and the server process. Thus an invalid cast means your clients are using an unsupported interface. In such cases, you usually want to fail fast and signal an error.
- defmodule KeyValueStore do
- use GenServer
- def init(_) do
- {:ok , %{}}
- end
- def start do
- GenServer.start(KeyValueStore, nil)
- end
- 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 handle_cast({:put, key, value}, state) do
- { :noreply, Map.put(state, key, value) }
- end
- def handle_call({:get, key}, state) do
- { :reply, Map.get(state, key) }
- end
- def handle_info(_, state) do
- {:noreply, state}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement