Skip to content

Commit 39b862d

Browse files
committed
deprecate start functions, close #48
1 parent 0b059c5 commit 39b862d

File tree

9 files changed

+59
-71
lines changed

9 files changed

+59
-71
lines changed

README.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Add this to the dependencies:
1212

1313
```elixir
14-
{:exredis, ">= 0.2.1"}
14+
{:exredis, ">= 0.2.2"}
1515
```
1616

1717
---
@@ -21,13 +21,13 @@ Add this to the dependencies:
2121
In your applications config.exs file you need to add new section for customizing redis connection.
2222

2323
```elixir
24-
config :exredis,
25-
host: "127.0.0.1",
26-
port: 6379,
27-
password: "",
28-
db: 0,
29-
reconnect: :no_reconnect,
30-
max_queue: :infinity
24+
config :exredis,
25+
host: "127.0.0.1",
26+
port: 6379,
27+
password: "",
28+
db: 0,
29+
reconnect: :no_reconnect,
30+
max_queue: :infinity
3131
```
3232

3333
### Usage ([web docs](http://hexdocs.pm/exredis/))
@@ -38,8 +38,8 @@ __As mixin__
3838
defmodule Pi do
3939
import Exredis
4040

41-
def get, do: start |> query ["GET", "Pi"]
42-
def set, do: start |> query ["SET", "Pi", "3.14"]
41+
def get, do: start_link |> elem(1) |> query ["GET", "Pi"]
42+
def set, do: start_link |> elem(1) |> query ["SET", "Pi", "3.14"]
4343
end
4444

4545
Pi.set
@@ -61,7 +61,7 @@ defmodule Api do
6161

6262
end
6363

64-
client = Exredis.start
64+
{:ok, client} = Exredis.start_link
6565

6666
client |> Api.set
6767
# => "OK"
@@ -73,12 +73,9 @@ client |> Api.get
7373
__Connect to the Redis server__
7474

7575
```elixir
76-
client = Exredis.start
76+
{:ok, client} = Exredis.start_link
7777
# or
7878
client = Exredis.start_using_connection_string("redis://127.0.0.1:6379")
79-
# or
80-
{:ok, client} = Exredis.start_link
81-
8279
```
8380

8481
__Disconnect from the server__
@@ -132,8 +129,8 @@ client |> Exredis.query_pipe [["SET", :a, "1"], ["LPUSH", :b, "3"], ["LPUSH", :b
132129
__Pub/sub__
133130

134131
```elixir
135-
client_sub = Exredis.Sub.start
136-
client = Exredis.start
132+
{:ok, client_sub} = Exredis.Sub.start_link
133+
{:ok, client} = Exredis.start_link
137134
pid = Kernel.self
138135

139136
client_sub |> Exredis.Sub.subscribe "foo", fn(msg) ->
@@ -158,8 +155,8 @@ end
158155
__Pub/sub by a pattern__
159156

160157
```elixir
161-
client_sub = Exredis.Sub.start
162-
client = Exredis.start
158+
{:ok, client_sub} = Exredis.Sub.start_link
159+
{:ok, client} = Exredis.start_link
163160
pid = Kernel.self
164161

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

lib/exredis.ex

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Exredis do
44
"""
55

66
@type reconnect_sleep :: :no_reconnect | integer
7-
@type start_link :: { :ok, pid } | { :error, term }
7+
@type start_link :: {:ok, pid} | {:error, term}
88

99
@doc """
1010
Connects to the Redis server using a connection string:
@@ -17,33 +17,27 @@ defmodule Exredis do
1717
@spec start_using_connection_string(binary, :no_reconnect | integer) :: pid
1818
def start_using_connection_string(connection_string \\ "redis://127.0.0.1:6379", reconnect_sleep \\ :no_reconnect) do
1919
config = Exredis.Config.parse(connection_string)
20-
start(config.host, config.port, config.db, config.password, reconnect_sleep)
20+
start_link(config.host, config.port, config.db, config.password, reconnect_sleep) |> elem(1)
2121
end
2222

23-
@doc """
24-
Connects to the Redis server:
25-
26-
* `start("127.0.0.1", 6379)`
27-
* `start("127.0.0.1", 6379, 0, "", :no_reconnect)`
28-
29-
Returns the pid of the connected client.
30-
"""
23+
@doc false
3124
@spec start(binary, integer, integer, binary, :no_reconnect | integer) :: pid
3225
def start(host, port, database \\ 0,
33-
password \\ "", reconnect_sleep \\ :no_reconnect), do:
26+
password \\ "", reconnect_sleep \\ :no_reconnect) do
27+
IO.write :stderr, "warning: Exredis.start/5 is deprecated\n" <>
28+
Exception.format_stacktrace
29+
3430
start_link(host, port, database, password, reconnect_sleep)
3531
|> elem 1
32+
end
3633

3734

38-
@doc """
39-
Connects to the Redis server with default or environment settings:
40-
41-
* `start`
42-
43-
Returns the pid of the connected client.
44-
"""
35+
@doc false
4536
@spec start :: pid
4637
def start do
38+
IO.write :stderr, "warning: Exredis.start/0 is deprecated\n" <>
39+
Exception.format_stacktrace
40+
4741
config = Exredis.Config.fetch_env
4842
start_link(config.host, config.port, config.db, config.password, config.reconnect)
4943
|> elem 1

lib/exredis/api.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Exredis.Api do
3030
def defaultclient do
3131
pid = Process.whereis(:exredis_hapi_default_client)
3232
if !pid do
33-
pid = Exredis.start
33+
{:ok, pid} = Exredis.start_link
3434
Process.register pid, :exredis_hapi_default_client
3535
end
3636
pid

lib/exredis/sub.ex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Exredis.Sub do
66
@type reconnect :: :no_reconnect | integer
77
@type max_queue :: :infinity | integer
88
@type behaviour :: :drop | :exit
9-
@type start_link :: { :ok, pid } | { :error, term }
9+
@type start_link :: {:ok, pid} | {:error, term}
1010

1111
@doc """
1212
Connect to the Redis server for subscribe to a channel
@@ -28,26 +28,23 @@ defmodule Exredis.Sub do
2828
:eredis_sub.start_link(String.to_char_list(config.host), config.port, String.to_char_list(config.password), config.reconnect, config.max_queue, config.behaviour)
2929
end
3030

31-
@doc """
32-
Connect to the Redis server for subscribe to a channel
33-
34-
* `start("127.0.0.1", 6379)`
35-
* `start("127.0.0.1", 6379, "with_password")`
36-
"""
31+
@doc false
3732
@spec start(binary, integer, binary, reconnect, max_queue, behaviour) :: pid
3833
def start(host, port, password,
3934
reconnect, max_queue,
4035
behaviour) do
36+
IO.write :stderr, "warning: Exredis.Sub.start/6 is deprecated\n" <>
37+
Exception.format_stacktrace
38+
4139
start_link(host, port, password, reconnect, max_queue, behaviour)
4240
|> elem 1
4341
end
44-
@doc """
45-
Connect to the Redis server with default or env settings for subscribe to a channel
46-
47-
* `start`
48-
"""
42+
@doc false
4943
@spec start :: pid
5044
def start do
45+
IO.write :stderr, "warning: Exredis.Sub.start/0 is deprecated\n" <>
46+
Exception.format_stacktrace
47+
5148
config = Exredis.Config.fetch_env
5249

5350
start_link(config.host, config.port, config.password, config.reconnect, config.max_queue, config.behaviour)
@@ -69,7 +66,7 @@ defmodule Exredis.Sub do
6966
reconnect \\ :no_reconnect, max_queue \\ :infinity,
7067
behaviour \\ :drop) do
7168
config = Exredis.Config.parse(connection_string)
72-
start(config.host, config.port, config.password, reconnect, max_queue, behaviour)
69+
start_link(config.host, config.port, config.password, reconnect, max_queue, behaviour) |> elem(1)
7370
end
7471

7572

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Exredis.Mixfile do
22
use Mix.Project
33

4-
@version "0.2.1"
4+
@version "0.2.2"
55

66
def project do
77
[app: :exredis,

test/api_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule ApiTest do
1717
alias Exredis.Api, as: R
1818

1919
setup do
20-
client = E.start
20+
{:ok, client} = E.start_link
2121

2222
# clean up database and set test value
2323
client |> E.query ["FLUSHALL"]

test/exredis_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ defmodule Pi do
44
import Exredis
55

66
# set/get
7-
def get, do: start |> query ["GET", "Pi"]
8-
def set, do: start |> query ["SET", "Pi", "3.14"]
7+
def get, do: start_link |> elem(1) |> query ["GET", "Pi"]
8+
def set, do: start_link |> elem(1) |> query ["SET", "Pi", "3.14"]
99
end
1010

1111
defmodule ExredisTest do
1212
use ExUnit.Case, async: true
1313
alias Exredis, as: E
1414

1515
setup do
16-
client = E.start
16+
{:ok, client} = E.start_link
1717

1818
# clean up database and set test value
1919
client |> E.query ["FLUSHALL"]
@@ -35,11 +35,11 @@ defmodule ExredisTest do
3535
end
3636

3737
test "connect" do
38-
assert E.start |> is_pid
38+
assert E.start_link |> elem(1) |> is_pid
3939
end
4040

4141
test "connect, erlang way" do
42-
{ :ok, pid } = E.start_link
42+
{:ok, pid} = E.start_link
4343

4444
assert pid |> is_pid
4545
end
@@ -49,7 +49,7 @@ defmodule ExredisTest do
4949
end
5050

5151
test "disconnect" do
52-
assert (E.start |> E.stop) == :ok
52+
assert (E.start_link |> elem(1) |> E.stop) == :ok
5353
end
5454

5555
test "set returns OK", ctx do
@@ -81,7 +81,7 @@ defmodule ExredisTest do
8181
end
8282

8383
test "transactions" do
84-
client = E.start
84+
{:ok, client} = E.start_link
8585

8686
status = E.query(client, ["MULTI"])
8787
assert status == "OK"
@@ -101,7 +101,7 @@ defmodule ExredisTest do
101101

102102
test "pipelining" do
103103
query = [["SET", :a, "1"], ["LPUSH", :b, "3"], ["LPUSH", :b, "2"]]
104-
client = E.start
104+
{:ok, client} = E.start_link
105105

106106
status = E.query_pipe(client, query)
107107
assert status == ["OK", "1", "2"]

test/pubsub_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ defmodule PubsubTest do
77
alias Exredis.Api, as: R
88

99
test "connect" do
10-
assert S.start |> is_pid
10+
assert S.start_link |> elem(1) |> is_pid
1111
end
1212

1313
test "connect, erlang way" do
14-
{ :ok, pid } = S.start_link
14+
{:ok, pid} = S.start_link
1515

1616
assert pid |> is_pid
1717
end
@@ -21,12 +21,12 @@ defmodule PubsubTest do
2121
end
2222

2323
test "disconnect" do
24-
assert (S.start |> S.stop) == :ok
24+
assert (S.start_link |> elem(1) |> S.stop) == :ok
2525
end
2626

2727
test "pub/sub" do
28-
client_sub = S.start
29-
client = E.start
28+
{:ok, client_sub} = S.start_link
29+
{:ok, client} = E.start_link
3030
pid = Kernel.self
3131

3232
client_sub |> S.subscribe "foo", fn(msg) ->
@@ -53,8 +53,8 @@ defmodule PubsubTest do
5353

5454

5555
test "pub/sub with multiple channels" do
56-
client_sub = S.start
57-
client = E.start
56+
{:ok, client_sub} = S.start_link
57+
{:ok, client} = E.start_link
5858
pid = Kernel.self
5959

6060
client_sub |> S.subscribe ["foo", "bar"], fn(msg) ->
@@ -86,8 +86,8 @@ defmodule PubsubTest do
8686

8787

8888
test "pub/sub by a pattern" do
89-
client_sub = S.start
90-
client = E.start
89+
{:ok, client_sub} = S.start_link
90+
{:ok, client} = E.start_link
9191
pid = Kernel.self
9292

9393
client_sub |> S.psubscribe "bar_*", fn(msg) ->

test/script_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule ScriptTest do
1717
@lua_script_sha_non_existing "9c609a7ccf8e2ef9459f7f88c414c951e3d174d9"
1818

1919
setup do
20-
client = E.start
20+
{:ok, client} = E.start_link
2121

2222
# clean up database and set test value
2323
client |> E.query ["FLUSHALL"]

0 commit comments

Comments
 (0)