redic 1.2.0 → 1.3.0

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: 1a5382b81b79200b8cfb791f454b9089b06ace3b
4
- data.tar.gz: 7c893f68df454916127d6b8ef6ab079918745b28
3
+ metadata.gz: 9dc763e03ae47f86f5bfeef03c3e6b041a3cefd4
4
+ data.tar.gz: 5e0caf8544cd4501df91a4125375bd8ebebf08f3
5
5
  SHA512:
6
- metadata.gz: f0dc33eb8a5874ceda6f206b880feefd7a1ee7c9614620f95adc0d382fdcd225b0223d95c8865aa738239aa5df73f7328bb5fdb70beb236c18a76c56641b7008
7
- data.tar.gz: 5ae3b1abd97cee063cce9d4bdb29ffe2faae66562251533186a9b2016031a287802f3905cbfd6a1ff429278459300f73ec37fd0791d42e3c0d5635d6e421c066
6
+ metadata.gz: adc94f3aaf1b6ea29e735d26ece898d910419f87eb3edcb7998584aedb7a213342254e2e1be0b6dcac12fc6068c8507c84ed1c895c0d89c4f884998194edf143
7
+ data.tar.gz: 7a602b0fc1b8dc193b33f3f0d501f9e6eb7f0b550159ea77ec85d690b9599ce0dec279ca553f6d0779e3dc2d7ac461f6cd789c7475f79f434dcc55d19c880712
@@ -7,7 +7,19 @@ class Redic
7
7
  def initialize(url = "redis://127.0.0.1:6379", timeout = 10_000_000)
8
8
  @url = url
9
9
  @client = Redic::Client.new(url, timeout)
10
- @queue = []
10
+ @buffer = Hash.new { |h, k| h[k] = [] }
11
+ end
12
+
13
+ def buffer
14
+ @buffer[Thread.current.object_id]
15
+ end
16
+
17
+ def reset
18
+ @buffer.delete(Thread.current.object_id)
19
+ end
20
+
21
+ def clear
22
+ @buffer.clear
11
23
  end
12
24
 
13
25
  def configure(url, timeout = 10_000_000)
@@ -23,21 +35,21 @@ class Redic
23
35
  end
24
36
 
25
37
  def queue(*args)
26
- @queue << args
38
+ buffer << args
27
39
  end
28
40
 
29
41
  def commit
30
42
  @client.connect do
31
- @queue.each do |args|
43
+ buffer.each do |args|
32
44
  @client.write(args)
33
45
  end
34
46
 
35
- @queue.map do
47
+ buffer.map do
36
48
  @client.read
37
49
  end
38
50
  end
39
51
  ensure
40
- @queue.clear
52
+ reset
41
53
  end
42
54
 
43
55
  def timeout
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "redic"
5
- s.version = "1.2.0"
5
+ s.version = "1.3.0"
6
6
  s.summary = "Lightweight Redis Client"
7
7
  s.description = "Lightweight Redis Client"
8
8
  s.authors = ["Michel Martens", "Cyril David"]
@@ -0,0 +1,46 @@
1
+ require "cutest"
2
+ require_relative "../lib/redic"
3
+
4
+ REDIS_URL = "redis://localhost:6379/"
5
+
6
+ prepare do
7
+ Redic.new(REDIS_URL).call("FLUSHDB")
8
+ end
9
+
10
+ test "multiple threads" do
11
+
12
+ cs = Array.new
13
+
14
+ c = Redic.new(REDIS_URL)
15
+
16
+ c.queue("SET", "foo", "1")
17
+
18
+ t1 = Thread.new do
19
+ c.queue("SET", "bar", "2")
20
+ end
21
+
22
+ t2 = Thread.new do
23
+ c.queue("SET", "baz", "3")
24
+ c.commit
25
+ end
26
+
27
+ t1.join
28
+ t2.join
29
+
30
+ assert_equal nil, c.call("GET", "foo")
31
+ assert_equal nil, c.call("GET", "bar")
32
+ assert_equal "3", c.call("GET", "baz")
33
+
34
+ c.commit
35
+
36
+ # The buffer for `c` still exists
37
+ assert_equal "1", c.call("GET", "foo")
38
+
39
+ # Buffer for the thread that didn't commit is the only one left
40
+ assert_equal 1, c.instance_variable_get("@buffer").keys.size
41
+
42
+ c.clear
43
+
44
+ # All buffers are cleared
45
+ assert_equal 0, c.instance_variable_get("@buffer").keys.size
46
+ 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: 1.2.0
4
+ version: 1.3.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: 2015-01-26 00:00:00.000000000 Z
12
+ date: 2015-02-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hiredis
@@ -43,6 +43,7 @@ files:
43
43
  - lib/redic/connection.rb
44
44
  - makefile
45
45
  - redic.gemspec
46
+ - tests/multithreaded_test.rb
46
47
  - tests/redic_test.rb
47
48
  - tests/unix_test.rb
48
49
  homepage: https://github.com/amakawa/redic