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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bbf2e9c0a04c38c2fedc078ec27d1f92f5c6d4d
4
- data.tar.gz: 9e58dc7004c43eceee2d4207551bd48aad7ecebb
3
+ metadata.gz: 982edbdc6b3b3ca417621ca43967117805d0faa9
4
+ data.tar.gz: 2f4b5a8ee99890f5e432cf5606984a3f683303de
5
5
  SHA512:
6
- metadata.gz: 89892ba684ac2c9ca56d433feadf3871d3dd89a541e850fe717bf9c067e6e0d84730f73f7e9d7dfde5b3ca950ef173a22c461356eedf7e4a5c09abab101bd1d7
7
- data.tar.gz: 268b97ee191a5092dc63ca8658cc5af6eaf66ebf75413119e7255643300934c0554ca18bcf592c2f64d15927264b7813c70d98a8a81031866bf37c21b51779a5
6
+ metadata.gz: b4e520979e52233b4137c02463ea4fd6d1a7a222e3ec6665999fce604ed534a99ef36d05253bcce0255f7fec16a557dfc6b81f78e340c2dceb8a70b6ed9694e5
7
+ data.tar.gz: 080f253def0a1a5d0c7357a81b989a523a557e40de9b1d254fa0ef96c993b2738ed9e81be8656e27a031ff2ae7c3e1648a18f83c133c8a84c07e52a423965541
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Michel Martens, Cyril David
1
+ Copyright (c) 2013-2014 Michel Martens, Cyril David
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
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#run
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
@@ -1,12 +1,12 @@
1
- require "redic/client"
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
@@ -1,10 +1,13 @@
1
- require "redic/connection"
1
+ require_relative "connection"
2
2
  require "uri"
3
3
 
4
4
  class Redic
5
5
  class Client
6
- def initialize(url)
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 [:auth, @uri.password]
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
@@ -2,15 +2,13 @@ require "hiredis/connection"
2
2
 
3
3
  class Redic
4
4
  module Connection
5
- TIMEOUT = 10_000_000
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, TIMEOUT)
9
+ connection.connect_unix(uri.path, timeout)
12
10
  else
13
- connection.connect(uri.host, uri.port, TIMEOUT)
11
+ connection.connect(uri.host, uri.port, timeout)
14
12
  end
15
13
 
16
14
  connection
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "redic"
5
- s.version = "0.0.8"
5
+ s.version = "1.0.0"
6
6
  s.summary = "Lightweight Redis Client"
7
7
  s.description = "Lightweight Redis Client"
8
8
  s.authors = ["Michel Martens", "Cyril David"]
@@ -1,6 +1,7 @@
1
- require File.expand_path("../lib/redic", File.dirname(__FILE__))
1
+ require "cutest"
2
+ require_relative "../lib/redic"
2
3
 
3
- REDIS_URL = "redis://localhost:6379/3"
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/3", c.url
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|
@@ -1,4 +1,5 @@
1
- require File.expand_path("../lib/redic", File.dirname(__FILE__))
1
+ require "cutest"
2
+ require_relative "../lib/redic"
2
3
 
3
4
  setup do
4
5
  Redic.new("unix:///tmp/redis.sock")
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.8
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: 2013-10-16 00:00:00.000000000 Z
12
+ date: 2014-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hiredis