redic 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22a26d943e92d11d2dbe989f1b7439453376b2e5
4
- data.tar.gz: c9f7001a56b5a853282339e5dd3976dceb7bedd9
3
+ metadata.gz: 826a4619b6aec694f3b74b8221db03a6e4cf8f61
4
+ data.tar.gz: 7c3635c0daf967b9567c9c9f92b8ad5b7ec09284
5
5
  SHA512:
6
- metadata.gz: 7d515e2ae172a21083ec61445828dac0c4edc5bea6599c8f525cfa9a6e97ff6c3e5e597b98232f7852c08cd79c5dc2b8e4318b8c96253617535bed108d045ce9
7
- data.tar.gz: 95d6eed5fb6210f0147ae46fb4f4940c218dfef27242f12e1ea628d3a19298e023ac6b9db4110b7ca8e7ae40f9763d31fe5187ff52c846d86413968da59d0e47
6
+ metadata.gz: 53ee3848f37877d86f789e8883867b5f4d6daec0f6575df993e291b343829089471b986a529d09da358316c136b824209e47bebb23604d85da57ef26e99e53d4
7
+ data.tar.gz: df61e6d8c6551297490450d0001dc2b68691d0008321bfb296d34ed41048fce8f1f0e28163a13b619870c78d13651d8fe2971136e2ae850a5fe1ea85689bf0c2
data/.gems ADDED
@@ -0,0 +1,2 @@
1
+ cutest -v 1.2.0
2
+ hiredis -v 0.4.5
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /.gs
data/README.md CHANGED
@@ -6,8 +6,8 @@ Lightweight Redis Client
6
6
  Description
7
7
  -----------
8
8
 
9
- Wrapper for `Redis::Client` that avoids rubyisms. It is inspired
10
- by [redigo](https://github.com/garyburd/redigo), a Redis client
9
+ Lightweight Redis Client inspired by
10
+ [redigo](https://github.com/garyburd/redigo), a Redis client
11
11
  library for golang.
12
12
 
13
13
  ## Usage
@@ -17,16 +17,16 @@ library for golang.
17
17
  redis = Redic.new
18
18
 
19
19
  # Processes the command and returns the response.
20
- c.call("SET", "foo", "bar")
20
+ redis.call("SET", "foo", "bar")
21
21
 
22
- assert_equal "bar", c.call("GET", "foo")
22
+ assert_equal "bar", redis.call("GET", "foo")
23
23
 
24
24
  # Pipelining is implemented by buffering commands,
25
25
  # then calling Redic#run
26
- c.write("SET", "foo", "bar")
27
- c.write("GET", "foo")
26
+ redis.write("SET", "foo", "bar")
27
+ redis.write("GET", "foo")
28
28
 
29
- assert_equal ["OK", "bar"], c.run
29
+ assert_equal ["OK", "bar"], redis.run
30
30
  ```
31
31
 
32
32
  ## Installation
data/lib/redic.rb CHANGED
@@ -1,13 +1,16 @@
1
- require "redis"
1
+ require "redic/client"
2
2
 
3
3
  class Redic
4
- def initialize(options = {})
5
- @client = Redis::Client.new(options)
4
+ attr :url
5
+
6
+ def initialize(url = "redis://127.0.0.1:6379")
7
+ @url = url
8
+ @client = Redic::Client.new(url)
6
9
  @buffer = []
7
10
  end
8
11
 
9
12
  def call(*args)
10
- @client.send(:ensure_connected) do
13
+ @client.connect do
11
14
  @client.write(args)
12
15
  @client.read
13
16
  end
@@ -18,7 +21,7 @@ class Redic
18
21
  end
19
22
 
20
23
  def run
21
- @client.send(:ensure_connected) do
24
+ @client.connect do
22
25
  @buffer.each do |args|
23
26
  @client.write(args)
24
27
  end
@@ -0,0 +1,71 @@
1
+ require "redic/connection"
2
+ require "uri"
3
+
4
+ class Redic
5
+ class Client
6
+ def initialize(url)
7
+ @uri = URI.parse(url)
8
+ @connection = nil
9
+ @semaphore = Mutex.new
10
+ end
11
+
12
+ def read
13
+ @connection.read.tap do |reply|
14
+ raise reply if reply.kind_of?(RuntimeError)
15
+ end
16
+ end
17
+
18
+ def write(command)
19
+ @connection.write(command)
20
+ end
21
+
22
+ def connect
23
+ try(3) do
24
+ establish_connection unless connected?
25
+
26
+ @semaphore.synchronize do
27
+ yield
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+ def try(times)
34
+ tries = 0
35
+
36
+ begin
37
+ yield
38
+ rescue Exception => err
39
+ if (tries += 1) <= times
40
+ sleep 0.01
41
+ retry
42
+ else
43
+ raise err, "%s (retries=%d)" % [err.message, tries]
44
+ end
45
+ end
46
+ end
47
+
48
+ def establish_connection
49
+ begin
50
+ @connection = Redic::Connection.new(@uri)
51
+ rescue Exception => err
52
+ raise err, "Can't connect to: %s" % @uri
53
+ end
54
+
55
+ authenticate
56
+ end
57
+
58
+ def authenticate
59
+ if @uri.password
60
+ @semaphore.synchronize do
61
+ write [:auth, @uri.password]
62
+ read
63
+ end
64
+ end
65
+ end
66
+
67
+ def connected?
68
+ @connection && @connection.connected?
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,19 @@
1
+ require "hiredis/connection"
2
+
3
+ class Redic
4
+ module Connection
5
+ TIMEOUT = 10_000_000
6
+
7
+ def self.new(uri)
8
+ connection = Hiredis::Connection.new
9
+
10
+ if uri.scheme == "unix"
11
+ connection.connect_unix(uri.path, TIMEOUT)
12
+ else
13
+ connection.connect(uri.host, uri.port, TIMEOUT)
14
+ end
15
+
16
+ connection
17
+ end
18
+ end
19
+ end
data/makefile ADDED
@@ -0,0 +1,2 @@
1
+ build:
2
+ RUBYLIB=./lib cutest test/*.rb
data/redic.gemspec CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "redic"
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
  s.summary = "Lightweight Redis Client"
7
- s.description = "Wrapper for Redis::Client that avoids rubyisms."
7
+ s.description = "Lightweight Redis Client"
8
8
  s.authors = ["Michel Martens", "Cyril David"]
9
- s.email = ["michel@soveran.com", "me@cyrildavid.com"]
9
+ s.email = ["michel@soveran.com", "cyx@cyx.is"]
10
10
  s.homepage = "https://github.com/amakawa/redic"
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.license = "MIT"
13
- s.add_dependency "redis"
13
+
14
+ s.add_dependency "hiredis"
14
15
  end
data/test/redic_test.rb CHANGED
@@ -4,23 +4,33 @@ setup do
4
4
  Redic.new
5
5
  end
6
6
 
7
+ test "url" do |c|
8
+ assert_equal "redis://127.0.0.1:6379", c.url
9
+ end
10
+
7
11
  test "normal commands" do |c|
8
- c.call("SET", "foo", "bar")
12
+ c.call("SET", "foo", "bar")
9
13
 
10
14
  assert_equal "bar", c.call("GET", "foo")
11
15
  end
12
16
 
13
17
  test "pipelining" do |c|
14
- c.write("SET", "foo", "bar")
18
+ c.write("SET", "foo", "bar")
15
19
  c.write("GET", "foo")
16
20
 
17
21
  assert_equal ["OK", "bar"], c.run
18
22
  end
19
23
 
20
24
  test "multi/exec" do |c|
21
- c.write("MULTI")
22
- c.write("SET", "foo", "bar")
25
+ c.write("MULTI")
26
+ c.write("SET", "foo", "bar")
23
27
  c.write("EXEC")
24
28
 
25
29
  assert_equal ["OK", "QUEUED", ["OK"]], c.run
26
30
  end
31
+
32
+ test "runtime errors" do |c|
33
+ assert_raise RuntimeError do
34
+ c.call("KABLAMMO")
35
+ end
36
+ end
data/test/unix_test.rb ADDED
@@ -0,0 +1,26 @@
1
+ require File.expand_path("../lib/redic", File.dirname(__FILE__))
2
+
3
+ setup do
4
+ Redic.new("unix:///tmp/redis.sock")
5
+ end
6
+
7
+ test "normal commands" do |c|
8
+ c.call("SET", "foo", "bar")
9
+
10
+ assert_equal "bar", c.call("GET", "foo")
11
+ end
12
+
13
+ test "pipelining" do |c|
14
+ c.write("SET", "foo", "bar")
15
+ c.write("GET", "foo")
16
+
17
+ assert_equal ["OK", "bar"], c.run
18
+ end
19
+
20
+ test "multi/exec" do |c|
21
+ c.write("MULTI")
22
+ c.write("SET", "foo", "bar")
23
+ c.write("EXEC")
24
+
25
+ assert_equal ["OK", "QUEUED", ["OK"]], c.run
26
+ end
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-04-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: redis
15
+ name: hiredis
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - '>='
@@ -25,19 +25,25 @@ dependencies:
25
25
  - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
- description: Wrapper for Redis::Client that avoids rubyisms.
28
+ description: Lightweight Redis Client
29
29
  email:
30
30
  - michel@soveran.com
31
- - me@cyrildavid.com
31
+ - cyx@cyx.is
32
32
  executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
+ - .gems
37
+ - .gitignore
36
38
  - LICENSE
37
39
  - README.md
38
40
  - lib/redic.rb
41
+ - lib/redic/client.rb
42
+ - lib/redic/connection.rb
43
+ - makefile
39
44
  - redic.gemspec
40
45
  - test/redic_test.rb
46
+ - test/unix_test.rb
41
47
  homepage: https://github.com/amakawa/redic
42
48
  licenses:
43
49
  - MIT