Created
April 3, 2018 13:49
-
-
Save blackode/3f3c5c579c8863f35e26bac0e7a4bd2a to your computer and use it in GitHub Desktop.
State preserving after server collapse
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
defmodule Bank.Cache do | |
use GenServer | |
def start_link initial_balance do | |
GenServer.start_link(__MODULE__, initial_balance) | |
end | |
def init(initial_balance) do | |
{:ok, initial_balance} | |
end | |
def get_balance(pid) do | |
GenServer.call pid, :get | |
end | |
def save_balance(new_balance, pid) do | |
GenServer.cast(pid,{:save, new_balance}) | |
end | |
def handle_call(:get, _from, balance) do | |
{:reply, balance, balance} | |
end | |
def handle_cast({:save, new_balance}, _balance) do | |
{:noreply, new_balance} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment