Redis client for Elixir.
Add this to the dependencies:
{:exredis, ">= 0.2.4"}
In your applications config.exs file you need to add new section for customizing redis connection.
config :exredis,
host: "127.0.0.1",
port: 6379,
password: "",
db: 0,
reconnect: :no_reconnect,
max_queue: :infinity
or
config :exredis,
url: "redis://user:password@host:1234/10",
reconnect: :no_reconnect,
max_queue: :infinity
Usage (web docs)
As mixin
Warning: this example should not be used in production. It spawns new connection to redis on each call, and these connections will not be closed.
defmodule Pi do
import Exredis
def get, do: start_link |> elem(1) |> query ["GET", "Pi"]
def set, do: start_link |> elem(1) |> query ["SET", "Pi", "3.14"]
end
Pi.set
# => "OK"
Pi.get
# => "3.14"
defmodule Api do
import Exredis.Api
def set(client), do:
client |> set "key", "value"
def get(client), do:
client |> get "key"
end
{:ok, client} = Exredis.start_link
client |> Api.set
# => "OK"
client |> Api.get
# => "value"
Using Exredis.Api with multiple keys
{:ok, client} = Exredis.start_link
client |> Exredis.Api.sadd("set1", "bar")
# => "1"
client |> Exredis.Api.sadd("set2", ["bar", "foo"])
# => "2"
client |> Exredis.Api.sdiff(["set2", "set1"])
# => ["foo"]
client |> Exredis.Api.sdiffstore("dest", ["set2", "set1"])
# => "1"
Connect to the Redis server
{:ok, client} = Exredis.start_link
# or
client = Exredis.start_using_connection_string("redis://127.0.0.1:6379")
Disconnect from the server
client |> Exredis.stop
Set & Get
# set
client |> Exredis.query ["SET", "FOO", "BAR"]
# get
client |> Exredis.query ["GET", "FOO"]
# => "BAR"
Mset & Mget
# mset
client |> Exredis.query ["MSET" | ["key1", "value1", "key2", "value2", "key3", "value3"]]
# mget
client |> Exredis.query ["MGET" | ["key1", "key2", "key3"]]
# => ["value1", "value2", "value3"]
Transactions
# start
client |> Exredis.query ["MULTI"]
# exec
client |> Exredis.query ["SET", "foo", "bar"]
client |> Exredis.query ["SET", "bar", "baz"]
# commit
client |> Exredis.query ["EXEC"]
Pipelining
client |> Exredis.query_pipe [["SET", :a, "1"], ["LPUSH", :b, "3"], ["LPUSH", :b, "2"]]
Pub/sub
{:ok, client_sub} = Exredis.Sub.start_link
{:ok, client} = Exredis.start_link
pid = Kernel.self
client_sub |> Exredis.Sub.subscribe "foo", fn(msg) ->
send(pid, msg)
end
receive do
msg ->
IO.inspect msg
# => {:subscribed, "foo", #PID<0.85.0>}
end
client |> Exredis.Api.publish "foo", "Hello World!"
receive do
msg ->
IO.inspect msg
# => {:message, "foo", "Hello World!", #PID<0.85.0>}
end
Pub/sub by a pattern
{:ok, client_sub} = Exredis.Sub.start_link
{:ok, client} = Exredis.start_link
pid = Kernel.self
client_sub |> Exredis.Sub.psubscribe "bar_*", fn(msg) ->
send(pid, msg)
end
receive do
msg ->
IO.inspect msg
# => {:subscribed, "bar_*", #PID<0.104.0>}
end
client |> Exredis.Api.publish "bar_test", "Hello World!"
receive do
msg ->
IO.inspect msg
# => {:pmessage, "bar_*", "bar_test", "Hello World!", #PID<0.104.0>}
end
Scripting
client |> Exredis.Api.script_load "return 1 + 1"
# => "c301e0c5bc3538d2bad3fdbf2e281887e643ada4"
client |> Exredis.Api.evalsha "c301e0c5bc3538d2bad3fdbf2e281887e643ada4", 0, ["key1"], ["argv1"]
# => "2"
defmodule MyScripts do
import Exredis.Script
defredis_script :lua_echo, "return ARGV[1]"
defredis_script :huge_command, file_path: "lua_scripts/huge_command.lua"
end
client |> MyScripts.lua_echo(["mykey"], ["foo"])
# => "foo"
Supervised example
defmodule SomeApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(SomeApp.RedisRepo, [:myredis, "redis://localhost:6379/0"]),
]
opts = [strategy: :one_for_one, name: SomeApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule SomeApp.RedisRepo do
def start_link(name, uri) do
client = Exredis.start_using_connection_string(uri)
true = Process.register(client, name)
{:ok, client}
end
end
- 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