ram (ram v0.5.0) View Source
Link to this section Summary
Functions
Adds Node to an existing Ram cluster.
Looks up a Key.
Equivalent to get(Key, undefined).
Returns the Key's Value or Default if the Key is not found.
Starts Ram manually.
Atomically updates a Key with the given function.
Link to this section Functions
Specs
add_node(Node :: node()) -> ok | {error, Reason :: term()}.
Adds Node to an existing Ram cluster.
This method is to be called when:- Adding a new node to the cluster.
- Restarting a previously stopped node of the cluster, so that it joins the cluster again.
restart_server/0
instead.
Specs
delete(Key :: term()) -> ok.
Specs
fetch(Key :: term()) -> {ok, Value :: term()} | error.
Looks up a Key.
Returnserror
if the Key is not found.
Specs
get(Key :: term()) -> Value :: term().
Equivalent to get(Key, undefined).
Specs
get(Key :: term(), Default :: term()) -> Value :: term().
Returns the Key's Value or Default if the Key is not found.
Examples
Elixir
iex(1)> :ram.get("key")
:undefined
iex(2)> :ram.get("key", "default")
"default"
iex(3)> :ram.put("key", "value")
:ok
iex(4)> :ram.get("key")
"value"
Erlang
1> ram:get("key").
undefined
2> ram:get("key", "default").
"default"
3> ram:put("key", "value").
ok
4> ram:get("key").
"value"
Specs
nodes() -> [node()].
Specs
put(Key :: term(), Value :: term()) -> ok.
Specs
remove_node(Node :: node()) -> ok | {error, Reason :: term()}.
Specs
restart_server() -> ok | {error, Reason :: term()}.
Specs
start() -> ok.
Starts Ram manually.
In most cases Ram will be started as one of your application's dependencies, however you may use this helper method to start it manually.Specs
start_cluster([node()]) -> ok | {error, Reason :: term()}.
Specs
stop() -> ok | {error, Reason :: term()}.
Specs
stop_cluster([node()]) -> ok | {error, Reason :: term()}.
Specs
update(Key :: term(), Default :: term(), function()) -> ok.
Atomically updates a Key with the given function.
If Key is found then the existing Value is passed to the fun and its result is used as the updated Value of Key. If Key is not found, Default is put as the Value of Key. The Default value will not be passed through the update function.
Passing functions as arguments might not be compatible across different Erlang versions, so if your cluster is composed of nodes running different Erlang versions do not use this method.
Examples
Elixir
iex(1)> update_fun = fn existing_value -> existing_value * 2 end
#Function<44.65746770/1 in :erl_eval.expr/5>
iex(2)> :ram.update("key", 10, update_fun)
ok
iex(3)> :ram.get("key")
10
iex(4)> :ram.update("key", 10, update_fun)
ok
iex(5)> :ram.get("key")
20
Erlang
1> UpdateFun = fun(ExistingValue) -> ExistingValue * 2 end.
#Fun<erl_eval.44.65746770>
2> ram:update("key", 10, UpdateFun).
ok
3> ram:get("key").
10
4> ram:update("key", 10, UpdateFun).
ok
5> ram:get("key").
20