Last active
October 6, 2017 12:55
-
-
Save elbrujohalcon/fb65294b4e98f3191e5c78cf3fae4178 to your computer and use it in GitHub Desktop.
Examples for my Blog Post
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
-module(kvs). | |
-behavior(gen_server). | |
-export([start/0, store/2, retrieve/1]). | |
-export([init/1, handle_call/3, handle_cast/2]). | |
start() -> gen_server:start({local, ?MODULE}, ?MODULE, #{}, []). | |
store(K, V) -> gen_server:cast(?MODULE, {store, K, V}). | |
retrieve(K) -> gen_server:call(?MODULE, {retrieve, K}). | |
init(#{}) -> {ok, #{}}. | |
handle_cast({store, K, V}, State) -> {noreply, State#{K => V}}. | |
handle_call({retrieve, K}, _From, State) -> {reply, maps:get(K, State, notfound), State}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment