ram (ram v0.5.0) View Source

Exposes all of the Key Value store APIs.

Link to this section Summary

Functions

Adds Node to an existing Ram cluster.

Deletes a Key.

Looks up a Key.

Equivalent to get(Key, undefined).

Returns the Key's Value or Default if the Key is not found.

Returns the nodes in the Ram cluster.
Puts a Value for a Key.
Removes Node from the Ram cluster.
Restart a previously stopped node of the cluster, so that it joins the cluster again.

Starts Ram manually.

Starts the Ram cluster.
Stops Ram manually.
Stops the Ram cluster.

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.
Note that when restarting a server it might be preferable to use restart_server/0 instead.

Specs

delete(Key :: term()) -> ok.
Deletes a Key.

Specs

fetch(Key :: term()) -> {ok, Value :: term()} | error.

Looks up a Key.

Returns error 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()].
Returns the nodes in the Ram cluster.

Specs

put(Key :: term(), Value :: term()) -> ok.
Puts a Value for a Key.

Specs

remove_node(Node :: node()) -> ok | {error, Reason :: term()}.
Removes Node from the Ram cluster.

Specs

restart_server() -> ok | {error, Reason :: term()}.
Restart a previously stopped node of the cluster, so that it joins the cluster again.

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()}.
Starts the Ram cluster.

Specs

stop() -> ok | {error, Reason :: term()}.
Stops Ram manually.

Specs

stop_cluster([node()]) -> ok | {error, Reason :: term()}.
Stops the Ram cluster.
Link to this function

update(Key, Default, Fun)

View Source

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