Redis client for Elixir
Add this to the dependencies:
# edge
{ :exredis, "0.0.2", [ github: "artemeff/exredis" ] }
# stable
{ :exredis, "0.0.1", [ github: "artemeff/exredis", tag: "0.0.1" ] }
As mixin
defmodule Pi do
use Exredis
def get, do: start |> query ["GET", "Pi"]
def set, do: start |> query ["SET", "Pi", "3.14"]
end
Pi.set
# => "OK"
Pi.get
# => "3.14"
Connect to the Redis server
client = Exredis.start
Disconnect from the server
Exredis.stop client
Set & Get
# set
Exredis.query(client, ["SET", "FOO", "BAR"])
# get
Exredis.query(client, ["GET", "FOO"])
# => "BAR"
Mset & Mget
# mset
Exredis.query(client, ["MSET" | ["key1", "value1", "key2", "value2", "key3", "value3"]])
# mget
Exredis.query(client, ["MGET" | ["key1", "key2", "key3"]])
# => ["value1","value2","value3"]
Transactions
# start
Exredis.query(client, ["MULTI"])
# exec
Exredis.query(client, ["SET", "foo", "bar"])
Exredis.query(client, ["SET", "bar", "baz"])
# commit
Exredis.query(client, ["EXEC"])
Pipelining
Exredis.query_pipe(client, [["SET", :a, "1"], ["LPUSH", :b, "3"], ["LPUSH", :b, "2"]])
Pub/sub
# subscribe, early documentation
# api should be improve, for now it's ugly
# define callback
def sub_callback(client, main_pid) do
receive do
msg ->
case msg do
{:subscribed, _channel, _pid} ->
#IO.inspect channel
#IO.inspect pid
main_pid <- "connect"
{:message, _channel, msg, _pid} ->
#IO.inspect channel
#IO.inspect msg
#IO.inspect pid
main_pid <- "message #{msg}"
_other -> nil
end
Exredis.Sub.ack_message client
sub_callback client, main_pid
end
end
# start client for subscribe
client_sub = Exredis.Sub.start
# subscribe!
Exredis.Sub.subscribe(client_sub, "foo", callback, Kernel.self)
# receive messages
receive do
msg -> IO.inspect msg
end
# publish
Exredis.Sub.publish(client, "foo", "bar")
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request