redic 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/redic.rb +17 -5
- data/redic.gemspec +1 -1
- data/tests/multithreaded_test.rb +46 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dc763e03ae47f86f5bfeef03c3e6b041a3cefd4
|
4
|
+
data.tar.gz: 5e0caf8544cd4501df91a4125375bd8ebebf08f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adc94f3aaf1b6ea29e735d26ece898d910419f87eb3edcb7998584aedb7a213342254e2e1be0b6dcac12fc6068c8507c84ed1c895c0d89c4f884998194edf143
|
7
|
+
data.tar.gz: 7a602b0fc1b8dc193b33f3f0d501f9e6eb7f0b550159ea77ec85d690b9599ce0dec279ca553f6d0779e3dc2d7ac461f6cd789c7475f79f434dcc55d19c880712
|
data/lib/redic.rb
CHANGED
@@ -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
|
-
@
|
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
|
-
|
38
|
+
buffer << args
|
27
39
|
end
|
28
40
|
|
29
41
|
def commit
|
30
42
|
@client.connect do
|
31
|
-
|
43
|
+
buffer.each do |args|
|
32
44
|
@client.write(args)
|
33
45
|
end
|
34
46
|
|
35
|
-
|
47
|
+
buffer.map do
|
36
48
|
@client.read
|
37
49
|
end
|
38
50
|
end
|
39
51
|
ensure
|
40
|
-
|
52
|
+
reset
|
41
53
|
end
|
42
54
|
|
43
55
|
def timeout
|
data/redic.gemspec
CHANGED
@@ -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.
|
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-
|
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
|