Git Product home page Git Product logo

exredis's Introduction

exredis Build Status Hex.pm


Redis client for Elixir.


Installation

Add this to the dependencies:

{ :exredis, ">= 0.1.1" }

Usage (web docs)

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"
defmodule Api do
  use Exredis.Api

  def set(client), do:
    client |> set "key", "value"

  def get(client), do:
    client |> get "key"

end

client = Exredis.start

client |> Api.set
# => "OK"

client |> Api.get
# => "value"

Connect to the Redis server

client = Exredis.start
# or
client = Exredis.start_using_connection_string("redis://127.0.0.1:6379")
# or
{:ok, client} = Exredis.start_link

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

client_sub = Exredis.Sub.start
client = Exredis.start
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

client_sub = Exredis.Sub.start
client = Exredis.start
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
  use 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"

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

exredis's People

Contributors

aforward avatar aidansteele avatar artemeff avatar behe avatar bitdeli-chef avatar davetron5000 avatar drouchy avatar ismaelga avatar joakimk avatar lastcanal avatar psli avatar rv-bcarter avatar stavro avatar whatyouhide avatar

Watchers

 avatar

Forkers

jclem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.