redic 0.0.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +34 -1
- data/lib/redic.rb +7 -3
- data/lib/redic/client.rb +17 -4
- data/lib/redic/connection.rb +3 -5
- data/redic.gemspec +1 -1
- data/tests/redic_test.rb +14 -3
- data/tests/unix_test.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 982edbdc6b3b3ca417621ca43967117805d0faa9
|
4
|
+
data.tar.gz: 2f4b5a8ee99890f5e432cf5606984a3f683303de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4e520979e52233b4137c02463ea4fd6d1a7a222e3ec6665999fce604ed534a99ef36d05253bcce0255f7fec16a557dfc6b81f78e340c2dceb8a70b6ed9694e5
|
7
|
+
data.tar.gz: 080f253def0a1a5d0c7357a81b989a523a557e40de9b1d254fa0ef96c993b2738ed9e81be8656e27a031ff2ae7c3e1648a18f83c133c8a84c07e52a423965541
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -21,13 +21,46 @@ redis.call("SET", "foo", "bar")
|
|
21
21
|
assert_equal "bar", redis.call("GET", "foo")
|
22
22
|
|
23
23
|
# Pipelining is implemented by buffering commands,
|
24
|
-
# then calling Redic#
|
24
|
+
# then calling Redic#commit
|
25
25
|
redis.queue("SET", "foo", "bar")
|
26
26
|
redis.queue("GET", "foo")
|
27
27
|
|
28
28
|
assert_equal ["OK", "bar"], redis.commit
|
29
29
|
```
|
30
30
|
|
31
|
+
You can provide the password and the database to be selected. The
|
32
|
+
format for Redis URLs is `redis://user:pass@host:port/db`. As
|
33
|
+
Redis only needs a password for authentication, the user can be
|
34
|
+
omitted:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# Connect to localhost:6380 using "bar" as password and use the
|
38
|
+
# database 2. Both AUTH and SELECT commands are issued after
|
39
|
+
# connecting. The user part of the URL is not provided.
|
40
|
+
redis = Redic.new("redis://:bar@localhost:6380/2")
|
41
|
+
```
|
42
|
+
|
43
|
+
It is also possible to configure a timeout for the connection. The
|
44
|
+
default timeout is 10 seconds.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# Timeout expressed in microseconds.
|
48
|
+
redis = Redic.new(timeout: 2_000_000)
|
49
|
+
redis.timeout == 2_000_000 #=> true
|
50
|
+
```
|
51
|
+
|
52
|
+
Here's one final example using both a Redis URL and a timeout:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# It's recommended to store the REDIS_URL as an environment
|
56
|
+
# variable. Use `fetch` to retrieve values that must be present,
|
57
|
+
# as it raises an error if the value is not found.
|
58
|
+
REDIS_URL = ENV.fetch("REDIS_URL")
|
59
|
+
REDIS_TIMEOUT = ENV.fetch("REDIS_TIMEOUT")
|
60
|
+
|
61
|
+
redis = Redic.new(REDIS_URL, timeout: REDIS_TIMEOUT)
|
62
|
+
```
|
63
|
+
|
31
64
|
## Differences with redis-rb
|
32
65
|
|
33
66
|
Redic uses [hiredis][hiredis] for the connection and for parsing
|
data/lib/redic.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
|
1
|
+
require_relative "redic/client"
|
2
2
|
|
3
3
|
class Redic
|
4
4
|
attr :url
|
5
5
|
attr :client
|
6
6
|
|
7
|
-
def initialize(url = "redis://127.0.0.1:6379")
|
7
|
+
def initialize(url = "redis://127.0.0.1:6379", timeout: 10_000_000)
|
8
8
|
@url = url
|
9
|
-
@client = Redic::Client.new(url)
|
9
|
+
@client = Redic::Client.new(url, timeout)
|
10
10
|
@queue = []
|
11
11
|
end
|
12
12
|
|
@@ -34,4 +34,8 @@ class Redic
|
|
34
34
|
ensure
|
35
35
|
@queue.clear
|
36
36
|
end
|
37
|
+
|
38
|
+
def timeout
|
39
|
+
@client.timeout
|
40
|
+
end
|
37
41
|
end
|
data/lib/redic/client.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
|
1
|
+
require_relative "connection"
|
2
2
|
require "uri"
|
3
3
|
|
4
4
|
class Redic
|
5
5
|
class Client
|
6
|
-
|
6
|
+
attr :timeout
|
7
|
+
|
8
|
+
def initialize(url, timeout)
|
7
9
|
@uri = URI.parse(url)
|
10
|
+
@timeout = timeout
|
8
11
|
@connection = nil
|
9
12
|
@semaphore = Mutex.new
|
10
13
|
end
|
@@ -31,18 +34,28 @@ class Redic
|
|
31
34
|
private
|
32
35
|
def establish_connection
|
33
36
|
begin
|
34
|
-
@connection = Redic::Connection.new(@uri)
|
37
|
+
@connection = Redic::Connection.new(@uri, @timeout)
|
35
38
|
rescue StandardError => err
|
36
39
|
raise err, "Can't connect to: %s" % @uri
|
37
40
|
end
|
38
41
|
|
39
42
|
authenticate
|
43
|
+
select
|
40
44
|
end
|
41
45
|
|
42
46
|
def authenticate
|
43
47
|
if @uri.password
|
44
48
|
@semaphore.synchronize do
|
45
|
-
write [
|
49
|
+
write ["AUTH", @uri.password]
|
50
|
+
read
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def select
|
56
|
+
if @uri.path
|
57
|
+
@semaphore.synchronize do
|
58
|
+
write ["SELECT", @uri.path[1..-1]]
|
46
59
|
read
|
47
60
|
end
|
48
61
|
end
|
data/lib/redic/connection.rb
CHANGED
@@ -2,15 +2,13 @@ require "hiredis/connection"
|
|
2
2
|
|
3
3
|
class Redic
|
4
4
|
module Connection
|
5
|
-
|
6
|
-
|
7
|
-
def self.new(uri)
|
5
|
+
def self.new(uri, timeout)
|
8
6
|
connection = Hiredis::Connection.new
|
9
7
|
|
10
8
|
if uri.scheme == "unix"
|
11
|
-
connection.connect_unix(uri.path,
|
9
|
+
connection.connect_unix(uri.path, timeout)
|
12
10
|
else
|
13
|
-
connection.connect(uri.host, uri.port,
|
11
|
+
connection.connect(uri.host, uri.port, timeout)
|
14
12
|
end
|
15
13
|
|
16
14
|
connection
|
data/redic.gemspec
CHANGED
data/tests/redic_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "cutest"
|
2
|
+
require_relative "../lib/redic"
|
2
3
|
|
3
|
-
REDIS_URL = "redis://localhost:6379/
|
4
|
+
REDIS_URL = "redis://localhost:6379/"
|
4
5
|
|
5
6
|
prepare do
|
6
7
|
Redic.new(REDIS_URL).call("FLUSHDB")
|
@@ -11,7 +12,17 @@ setup do
|
|
11
12
|
end
|
12
13
|
|
13
14
|
test "url" do |c|
|
14
|
-
assert_equal "redis://localhost:6379/
|
15
|
+
assert_equal "redis://localhost:6379/", c.url
|
16
|
+
end
|
17
|
+
|
18
|
+
test "timeout" do |c1|
|
19
|
+
# Default timeout is 10 seconds
|
20
|
+
assert_equal 10_000_000, c1.timeout
|
21
|
+
|
22
|
+
# Timeout configured to 200_000 microseconds
|
23
|
+
c2 = Redic.new(timeout: 200_000)
|
24
|
+
|
25
|
+
assert_equal 200_000, c2.timeout
|
15
26
|
end
|
16
27
|
|
17
28
|
test "normal commands" do |c|
|
data/tests/unix_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hiredis
|