Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Created April 26, 2013 21:28
Show Gist options
  • Save patrickgombert/5470573 to your computer and use it in GitHub Desktop.
Save patrickgombert/5470573 to your computer and use it in GitHub Desktop.
Key/Val Store in Elixir
defmodule Db do
use GenServer.Behaviour
## API
def start() do
:gen_server.start({:local, :db}, __MODULE__, [], [])
end
def store(key, val) do
:gen_server.call(:db, {:store, key, val})
end
def fetch(key) do
:gen_server.call(:db, {:fetch, key})
end
## Callbacks
def init([]) do
{:ok, HashDict.new}
end
def handle_call({:store, key, val}, _info, state) do
new_state = Dict.put(state, key, val)
{:reply, val, new_state}
end
def handle_call({:fetch, key}, _info, state) do
{:reply, Dict.get(state, key), state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment