Skip to content

Instantly share code, notes, and snippets.

@Altech
Created October 25, 2013 05:55
Show Gist options
  • Save Altech/7150004 to your computer and use it in GitHub Desktop.
Save Altech/7150004 to your computer and use it in GitHub Desktop.
simple kvs server in erlang.
-module(kvs).
-export([start/0, store/2, lookup/1]).
start() -> register(kvs, spawn(fun() -> loop() end)).
store(Key, Value) -> rpc({store, Key, Value}).
lookup(Key) -> rpc({lookup, Key}).
rpc(Q) ->
kvs ! {self(), Q},
receive
{kvs, Reply} ->
Reply
end.
loop() ->
receive
{From, {store, Key, Value}} ->
put(Key, {ok, Value}),
From ! {kvs, true},
loop();
{From, {lookup, Key}} ->
From ! {kvs, get(Key)},
loop()
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment