Skip to content

glejeune/exredis

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

exredis Build Status

Redis client for Elixir


Installation

Add this to the dependencies:

{ :exredis, "0.0.4", [ github: "artemeff/exredis", tag: "v0.0.4" ] }

Usage

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

client_sub = Exredis.Sub.start
client = Exredis.start
pid = Kernel.self

client_sub |> Exredis.Sub.subscribe "foo", fn(msg) ->
  pid <- msg
end

receive do
  msg ->
    IO.inspect msg
    # => { :subscribed, "foo", #PID<0.85.0> }
end

client |> Exredis.Sub.publish "foo", "Hello World!"

receive do
  msg ->
    IO.inspect msg
    # => { :message, "foo", "Hello World!", #PID<0.85.0> }
end

Pub/sub by a pattern

client_sub = Exredis.Sub.start
client = Exredis.start
pid = Kernel.self

client_sub |> Exredis.Sub.psubscribe "bar_*", fn(msg) ->
  pid <- msg
end

receive do
  msg ->
    IO.inspect msg
    # => { :subscribed, "bar_*", #PID<0.104.0> }
end

client |> Exredis.Sub.publish "bar_test", "Hello World!"

receive do
  msg ->
    IO.inspect msg
    # => { :pmessage, "bar_*", "bar_test", "Hello World!", #PID<0.104.0> }
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Redis client for Elixir

Resources

License

Stars

Watchers

Forks

Packages

No packages published