Skip to content

glejeune/exredis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

exredis Build Status

Redis client for Elixir


Installation

Add this to the dependencies:

{ :exredis, "0.0.2", [ github: "artemeff/exredis", tag: "v0.0.2" ] }

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

# 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} ->
          main_pid <- "connect"

        {:message, _channel, msg, _pid} ->
          main_pid <- "message #{msg}"

        _other -> nil
      end

      Exredis.Sub.ack_message client
      sub_callback client, main_pid
  end
end

# sub_callback as anonymous function
callback = function(sub_callback/2)

# 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")

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