Skip to content

Instantly share code, notes, and snippets.

@elbrujohalcon
Last active October 6, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbrujohalcon/fb65294b4e98f3191e5c78cf3fae4178 to your computer and use it in GitHub Desktop.
Save elbrujohalcon/fb65294b4e98f3191e5c78cf3fae4178 to your computer and use it in GitHub Desktop.
Examples for my Blog Post
-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