redis-pool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODM4MDE2ODAzNjVmMDMxODJlNmFhYTgyZGI5OGQyYzhhM2U2NzRiOA==
5
+ data.tar.gz: !binary |-
6
+ YmU5ODY5MTAyMTI1MmQxOWRkMzIyNzg0MGIyOGNlOGEwNDdlOWUyZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZWE1OWRiM2Y4NjU4Mzg2YjM4YTZkZjFkMGIyZWZlNjk1ZGI1ZmYwOGQ2YjA1
10
+ ZGFmYzBjODQ0NzJiZGMyY2Q3ZDVmNjIzYWZkN2E0ZGI2OGVmYmY2OWNlMWVj
11
+ ODgzMmMyMjZlNzc3OWU1ZGM5ZTg3MmU4YzZiZmQ5YjVhNGQ0NGQ=
12
+ data.tar.gz: !binary |-
13
+ YmY0Y2I0YTE2NmE5ZjBmMjg4ZTE5MWU5ZDRhMTRkNDE0MjkwNWEyYTExOTE0
14
+ Zjc0NTExM2NiNmQwOGM4Y2EwZTRmOWE4MzljOWJkNTdjYjQ3ZDVmOWQ4ZDQ3
15
+ MDc4ZjU0YTQxNWU5OTUyMDZkODgwOGI4ZmVmNTJmNTM1ZjRmMGU=
@@ -0,0 +1 @@
1
+ /.gs
@@ -0,0 +1,20 @@
1
+ Redis::Pool
2
+ ===========
3
+
4
+ A Redis connection pool.
5
+
6
+ Usage
7
+ -----
8
+
9
+ require "redis/pool"
10
+
11
+ $redis = Redis::Pool.new(size: 10)
12
+
13
+ Array.new(100) do
14
+ Thread.new do
15
+ $redis.get("foo")
16
+ end
17
+ end.each(&:join)
18
+
19
+ $redis.info["connected_clients"]
20
+ # => 10
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,56 @@
1
+ require "connection_pool"
2
+ require "redis"
3
+
4
+ class Redis::Pool < Redis
5
+ VERSION = "0.1.0"
6
+
7
+ attr :pool
8
+
9
+ def initialize(options = {})
10
+ @pool = ConnectionPool.new(size: options.delete(:size)) { Redis::Client.new(options) }
11
+ @id = "Redis::Pool::#{object_id}"
12
+
13
+ super
14
+ end
15
+
16
+ def synchronize
17
+ @pool.with do |client|
18
+ _with_client(client) { yield(client) }
19
+ end
20
+ end
21
+
22
+ def pipelined
23
+ pipeline = Pipeline.new
24
+
25
+ _with_client(pipeline) do |client|
26
+ yield(client)
27
+ end
28
+
29
+ synchronize do |client|
30
+ client.call_pipeline(pipeline)
31
+ end
32
+ end
33
+
34
+ def multi
35
+ raise ArgumentError, "Redis::Pool#multi can only be called with a block" unless block_given?
36
+
37
+ pipeline = Pipeline::Multi.new
38
+
39
+ _with_client(pipeline) do |client|
40
+ yield(client)
41
+ end
42
+
43
+ synchronize do |client|
44
+ client.call_pipeline(pipeline)
45
+ end
46
+ end
47
+
48
+ protected
49
+
50
+ def _with_client(client)
51
+ Thread.current[@id] = client
52
+ yield(client)
53
+ ensure
54
+ Thread.current[@id] = nil
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ task :test do
2
+ exec "cutest test/**/*_test.rb"
3
+ end
4
+
5
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ require "./lib/redis/pool"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "redis-pool"
5
+
6
+ s.version = Redis::Pool::VERSION
7
+
8
+ s.homepage = "https://github.com/djanowski/redis-pool"
9
+
10
+ s.summary = "A Redis connection pool."
11
+
12
+ s.authors = ["Damian Janowski"]
13
+
14
+ s.email = ["jano@dimaion.com"]
15
+
16
+ s.license = "Unlicense"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- test/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
21
+
22
+ s.add_dependency("connection_pool")
23
+ s.add_dependency("redis")
24
+
25
+ s.add_development_dependency("cutest")
26
+ end
@@ -0,0 +1,81 @@
1
+ require "cutest"
2
+
3
+ $VERBOSE = 1
4
+
5
+ require_relative "../lib/redis/pool"
6
+
7
+ at_exit do
8
+ if $redis
9
+ Process.kill(:TERM, $redis)
10
+ end
11
+
12
+ Process.waitpid
13
+ end
14
+
15
+ $redis = spawn("redis-server --port 9999 --logfile /dev/null")
16
+
17
+ def teardown(r)
18
+ r.pool.shutdown(&:disconnect)
19
+ end
20
+
21
+ setup do
22
+ Redis::Pool.new(port: 9999, size: 10)
23
+ end
24
+
25
+ test "Pool" do |r|
26
+ threads = Array.new(100) do
27
+ Thread.new do
28
+ 10.times do
29
+ r.get("foo")
30
+ end
31
+ end
32
+ end
33
+
34
+ threads.each(&:join)
35
+
36
+ assert_equal "10", r.info["connected_clients"]
37
+
38
+ teardown(r)
39
+ end
40
+
41
+ test "Pipelining" do |r|
42
+ threads = Array.new(100) do
43
+ Thread.new do
44
+ 10.times do
45
+ r.pipelined do
46
+ r.set("foo", "bar")
47
+
48
+ r.pipelined do
49
+ r.del("foo")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ threads += Array.new(100) do
57
+ Thread.new do
58
+ 10.times do
59
+ r.multi do
60
+ r.set("foo", "bar")
61
+ r.del("foo")
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ threads.each(&:join)
68
+
69
+ assert_equal "10", r.info["connected_clients"]
70
+ assert_equal nil, r.get("foo")
71
+
72
+ teardown(r)
73
+ end
74
+
75
+ test "MULTI fails when no block given" do |r|
76
+ assert_raise(ArgumentError) do
77
+ r.multi
78
+ end
79
+
80
+ teardown(r)
81
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cutest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - jano@dimaion.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - README.md
64
+ - UNLICENSE
65
+ - lib/redis/pool.rb
66
+ - rakefile
67
+ - redis-pool.gemspec
68
+ - test/pool_test.rb
69
+ homepage: https://github.com/djanowski/redis-pool
70
+ licenses:
71
+ - Unlicense
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.0
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A Redis connection pool.
93
+ test_files:
94
+ - test/pool_test.rb