Skip to content

Commit

Permalink
redis commands compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
artemeff committed Jan 13, 2019
1 parent ddf04c8 commit c0b3c39
Show file tree
Hide file tree
Showing 30 changed files with 1,045 additions and 1,685 deletions.
29 changes: 20 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/bench/graphs
/bench/snapshots
/doc
/ebin
/deps
/_build
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# OS
.DS_Store
Icon?
# Ignore commands in text elixir format
/priv/commands.exs
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Yuri Artemev
Copyright (c) 2019 Yuri Artemev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
242 changes: 5 additions & 237 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### exredis [![Build Status](https://img.shields.io/travis/artemeff/exredis.svg)](https://travis-ci.org/artemeff/exredis) [![Hex.pm](https://img.shields.io/hexpm/v/exredis.svg)](https://hex.pm/packages/exredis)
### Redis [![Build Status](https://img.shields.io/travis/artemeff/redis.svg)](https://travis-ci.org/artemeff/redis) [![Hex.pm](https://img.shields.io/hexpm/v/redis.svg)](https://hex.pm/packages/redis)

---

[Redis](http://redis.io) client for Elixir.
[Redis](http://redis.io) commands for Elixir. If you are looking for exredis, please check out [exredis](https://github.com/artemeff/exredis/tree/exredis) branch.

---

Expand All @@ -11,247 +11,15 @@
Add this to the dependencies:

```elixir
{:exredis, ">= 0.2.4"}
{:redis, "~> 0.1"}
```

---

### Configuration
### Usage ([API reference](http://hexdocs.pm/redis/))

In your applications config.exs file you need to add new section for customizing redis connection.
TODO

```elixir
config :exredis,
host: "127.0.0.1",
port: 6379,
password: "",
db: 0,
reconnect: :no_reconnect,
max_queue: :infinity
```

or

```elixir
config :exredis,
url: "redis://user:password@host:1234/10",
reconnect: :no_reconnect,
max_queue: :infinity
```

### Usage ([web docs](http://hexdocs.pm/exredis/))

__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.*

```elixir
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"
```

```elixir
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__

``` elixir
{: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__

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

__Disconnect from the server__

```elixir
client |> Exredis.stop
```

__Set & Get__

```elixir
# set
client |> Exredis.query ["SET", "FOO", "BAR"]

# get
client |> Exredis.query ["GET", "FOO"]
# => "BAR"
```

__Mset & Mget__

```elixir
# mset
client |> Exredis.query ["MSET" | ["key1", "value1", "key2", "value2", "key3", "value3"]]

# mget
client |> Exredis.query ["MGET" | ["key1", "key2", "key3"]]
# => ["value1", "value2", "value3"]
```

__Transactions__

```elixir
# start
client |> Exredis.query ["MULTI"]

# exec
client |> Exredis.query ["SET", "foo", "bar"]
client |> Exredis.query ["SET", "bar", "baz"]

# commit
client |> Exredis.query ["EXEC"]
```

__Pipelining__

```elixir
client |> Exredis.query_pipe [["SET", :a, "1"], ["LPUSH", :b, "3"], ["LPUSH", :b, "2"]]
```

__Pub/sub__

```elixir
{: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__

```elixir
{: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__

```elixir
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__

```elixir
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

```
---

### Contributing
Expand Down
72 changes: 72 additions & 0 deletions bench/commands.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Benchee.run(%{
"bitfield with all opts" => fn ->
[
"BITFIELD",
[" ", "key"],
[" ", ["GET", " ", ["type", " ", "offset"]]],
[" ", ["SET", " ", ["type", " ", "offset", " ", "value"]]],
[" ", ["INCRBY", " ", ["type", " ", "offset", " ", "increment"]]],
[" ", ["OVERFLOW", " ", "WRAP"]]
] =
Redis.bitfield("key", get: {"type", "offset"}, set: {"type", "offset", "value"},
incrby: {"type", "offset", "increment"}, overflow: :wrap)
end,

"georadius with opts as list" => fn ->
[
"GEORADIUSBYMEMBER",
[" ", "key"],
[" ", "member"],
[" ", "radius"],
[" ", "km"],
[" ", "WITHCOORD"],
[" ", "WITHDIST"],
[],
[" ", ["COUNT", " ", "1"]],
[" ", "ASC"],
[],
[]
] =
Redis.georadiusbymember("key", "member", "radius", :km,
[:asc, :withdist, :withcoord, count: "1"])
end,

"georadius with opts as keywords" => fn ->
[
"GEORADIUSBYMEMBER",
[" ", "key"],
[" ", "member"],
[" ", "radius"],
[" ", "km"],
[" ", "WITHCOORD"],
[" ", "WITHDIST"],
[],
[" ", ["COUNT", " ", "1"]],
[" ", "ASC"],
[],
[]
] =
Redis.georadiusbymember("key", "member", "radius", :km,
[order: :asc, withdist: :withdist, withcoord: :withcoord, count: "1"])
end,

"hmget" => fn ->
["HMGET", [" ", "key"], [" ", ["field1", " ", "field2"]]] =
Redis.hmget("key", ["field1", "field2"])
end,

"hmset" => fn ->
[
"HMSET",
[" ", "key"],
[" ", [["field1", " ", "value1"], " ", ["field2", " ", "value2"]]]
] =
Redis.hmset("key", [{"field1", "value1"}, {"field2", "value2"}])
end,

"set" => fn ->
["SET", [" ", "key"], [" ", "value"], [" ", ["PX", " ", "10000000"]], [" ", "XX"]] =
Redis.set("key", "value", [:xx, expiration: {:px, 1_000_0000}])
end,

}, time: 3, memory_time: 2, print: [fast_warning: false])
12 changes: 0 additions & 12 deletions bench/int_reply_bench.exs

This file was deleted.

Loading

0 comments on commit c0b3c39

Please sign in to comment.