stockpile-redis 1.0 → 1.1

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: 5265acd0adce155a74ad5e760a4220f2e67d21bd
4
- data.tar.gz: a0255909976359af85261d59d10b9b7cf50fc256
3
+ metadata.gz: c921c5dea37bc0ab0c8f2b5ebb77bb3db14071a3
4
+ data.tar.gz: 75fa3e0437603717c027d83270d36f93eb814852
5
5
  SHA512:
6
- metadata.gz: f4bcc22097af1e9ecda717d747a8462da5b7894ec795a27878a6f3bd44d6bd65eddba590b24f00064f20315f8f7be5bf6c44f5633f48824a620b431ab18a6880
7
- data.tar.gz: 5878c2e4459ad15f96b65948d2b724c0bd7062dbe63d44db9b1329edb661558d7297f391c6540d328e3d130674ac860698fef53d46dc48f9534d87be90615c9b
6
+ metadata.gz: bc72992d8cf5eaf4d146d0f3ae643659ea6b1c14d97a24bdaae96fa17b6fc2d74234dab6d79b39b3549d7ad16abae044784a2eff8e67717ddd8ff5917105116a
7
+ data.tar.gz: 061fe3aabb239931475bd1ba656b5d48e0d187c1f40777861844c9afa7e98be17a1b040256204193dc718a0bdde69d1e2fe13ba9780e9ef962c8f2c4d83204ca
data/History.rdoc CHANGED
@@ -1,3 +1,13 @@
1
+ === 1.1 / 2015-02-10
2
+
3
+ * 2 minor enhancements
4
+
5
+ * Modified Stockpile::Redis to be implmented based on the new Stockpile::Base
6
+ class provided in stockpile 1.1.
7
+
8
+ * Implemented the +namespace+ option for client connections to add additional
9
+ namespace support to individual child connections.
10
+
1
11
  === 1.0 / 2015-01-21
2
12
 
3
13
  * 1 major enhancement
data/Manifest.txt CHANGED
@@ -2,7 +2,6 @@
2
2
  .gemtest
3
3
  .minitest.rb
4
4
  .travis.yml
5
- .workenv
6
5
  Contributing.rdoc
7
6
  Gemfile
8
7
  History.rdoc
data/README.rdoc CHANGED
@@ -13,11 +13,14 @@ stockpile-redis is a connection manager for Redis to be used with
13
13
 
14
14
  stockpile-redis supports both normal Redis connections (<tt>Redis.new</tt>),
15
15
  and Redis::Namespace connections (<tt>Redis::Namespace.new('namespace', redis:
16
- Redis.new</tt>).
16
+ Redis.new)</tt>).
17
17
 
18
18
  stockpile-redis provides special-case handling for connections for Resque when
19
19
  the name of the client is +:resque+.
20
20
 
21
+ Release 1.1 adds a minor enhancement to allow for namespaces for named clients
22
+ using Stockpile::Redis#connection_for.
23
+
21
24
  == Synopsis
22
25
 
23
26
  require 'stockpile/redis'
@@ -39,6 +42,12 @@ the name of the client is +:resque+.
39
42
  narrow.connection_for(:resque) != narrow.connection # => true
40
43
  narrow.connection_for(:resque).redis == narrow.connection # => true
41
44
 
45
+ # Standard namespace handling.
46
+ narrow.connection_for(:other, namespace: 'other') !=
47
+ narrow.connection # => true
48
+ narrow.connection_for(:other, namespace: 'other').redis !=
49
+ narrow.connection # => true
50
+
42
51
  # Show a Stockpile with no adapter capabilities, but name the method
43
52
  # stockpile, not cache. This will still usefully manage connections.
44
53
  # The use of inject_redis! makes Stockpile::Redis the default connection
@@ -96,7 +105,7 @@ the name of the client is +:resque+.
96
105
 
97
106
  Put stockpile-redis in your Gemfile:
98
107
 
99
- gem 'stockpile-redis', '~> 1.0'
108
+ gem 'stockpile-redis', '~> 1.1'
100
109
 
101
110
  Or manually install:
102
111
 
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ spec = Hoe.spec 'stockpile-redis' do
22
22
  self.need_tar = true
23
23
  self.require_ruby_version '>= 1.9.2'
24
24
 
25
- self.extra_deps << ['stockpile', '~> 1.0']
25
+ self.extra_deps << ['stockpile', '~> 1.1']
26
26
  self.extra_deps << ['redis', '~> 3.0']
27
27
  self.extra_deps << ['redis-namespace', '~> 1.0']
28
28
  self.extra_dev_deps << ['fakeredis', '~> 0.5']
@@ -2,12 +2,12 @@
2
2
 
3
3
  require 'redis'
4
4
  require 'redis/namespace'
5
- require 'stockpile'
5
+ require 'stockpile/base'
6
6
 
7
7
  class Stockpile
8
8
  # A connection manager for Redis.
9
- class Redis
10
- VERSION = '1.0' # :nodoc:
9
+ class Redis < Stockpile::Base
10
+ VERSION = '1.1' # :nodoc:
11
11
 
12
12
  # Create a new Redis connection manager with the provided options.
13
13
  #
@@ -24,13 +24,11 @@ class Stockpile
24
24
  # <tt>options[:redis]</tt>, a namespace will be generated
25
25
  # from one of the following: <tt>$REDIS_NAMESPACE</tt>,
26
26
  # <tt>Rails.env</tt> (if in Rails), or <tt>$RACK_ENV</tt>.
27
- # +narrow+:: Use a narrow connection width if true; if not provided,
28
- # uses the value of ::Stockpile.narrow? in this connection
29
- # manager.
30
27
  def initialize(options = {})
31
- @redis_options = options.fetch(:redis, {})
32
- @narrow = !!options.fetch(:narrow, ::Stockpile.narrow?)
33
- @namespace = options.fetch(:namespace) {
28
+ super
29
+
30
+ @redis_options = (@options.delete(:redis) || {}).dup
31
+ @namespace = @options.fetch(:namespace) {
34
32
  @redis_options.fetch(:namespace) {
35
33
  ENV['REDIS_NAMESPACE'] ||
36
34
  (defined?(::Rails) && ::Rails.env) ||
@@ -38,23 +36,11 @@ class Stockpile
38
36
  }
39
37
  }
40
38
 
41
- if @redis_options.has_key?(:namespace)
42
- @redis_options = @redis_options.reject { |k, _| k == :namespace }
43
- end
44
-
45
- @connection = nil
46
- @clients = {}
47
- end
48
-
49
- # The current primary connection to Redis.
50
- attr_reader :connection
51
-
52
- # Indicates if this connection manager is using a narrow connection
53
- # width.
54
- def narrow?
55
- @narrow
39
+ @options.delete(:namespace)
40
+ @redis_options.delete(:namespace)
56
41
  end
57
42
 
43
+ ##
58
44
  # Connect to Redis, unless already connected. Additional client connections
59
45
  # can be specified in the parameters as a shorthand for calls to
60
46
  # #connection_for.
@@ -67,112 +53,136 @@ class Stockpile
67
53
  #
68
54
  # # This means the same as above.
69
55
  # manager.connect(:redis)
70
- def connect(*client_names)
71
- @connection ||= connect_for_any
72
-
73
- clients_from(*client_names).each { |client_name|
74
- connection_for(client_name)
75
- }
76
-
77
- connection
78
- end
56
+ #
57
+ # === Clients
58
+ #
59
+ # +clients+ may be provided in one of several ways:
60
+ #
61
+ # * A Hash object, mapping client names to client options.
62
+ #
63
+ # connect(redis: nil, rollout: { namespace: 'rollout' })
64
+ # # Transforms into:
65
+ # # connect(redis: {}, rollout: { namespace: 'rollout' })
66
+ #
67
+ # * An (implicit) array of client names, for connections with no options
68
+ # provided.
69
+ #
70
+ # connect(:redis, :resque, :rollout)
71
+ # # Transforms into:
72
+ # # connect(redis: {}, resque: {}, rollout: {})
73
+ #
74
+ # * An array of Hash objects, mapping client names to client options.
75
+ #
76
+ # connect({ redis: nil },
77
+ # { rollout: { namespace: 'rollout' } })
78
+ # # Transforms into:
79
+ # # connect(redis: {}, rollout: { namespace: 'rollout' })
80
+ #
81
+ # * A mix of client names and Hash objects:
82
+ #
83
+ # connect(:redis, { rollout: { namespace: 'rollout' } })
84
+ # # Transforms into:
85
+ # # connect(redis: {}, rollout: { namespace: 'rollout' })
86
+ #
87
+ # ==== Client Options
88
+ #
89
+ # Stockpile::Redis supports one option, +namespace+. The use of this with a
90
+ # client automatically wraps the client in the provided +namespace+. The
91
+ # namespace will be within the global namespace for the Stockpile::Redis
92
+ # instance, if one has been set.
93
+ #
94
+ # r = Stockpile::Redis.new(namespace: 'first')
95
+ # rr = r.client_for(other: { namespace: 'second' })
96
+ # rr.set 'found', true
97
+ # r.set 'found', true
98
+ # rr.keys # => [ 'found' ]
99
+ # r.keys # => [ 'found', 'second:found' ]
100
+ # r.connection.redis.keys # => [ 'first:found', 'first:second:found' ]
101
+ #
102
+ # :method: connect
79
103
 
80
- # Perform a client connection to Redis for a specific +client_name+. The
81
- # +client_name+ of +:all+ will always return +nil+.
104
+ ##
105
+ # Returns a Redis client connection for a particular client. If the
106
+ # connection manager is using a narrow connection width, this returns the
107
+ # same as #connection.
108
+ #
109
+ # The +client_name+ of +:all+ will always return +nil+.
82
110
  #
83
111
  # If the requested client does not yet exist, the connection will be
84
- # created.
112
+ # created with the provided options.
85
113
  #
86
114
  # Because Resque depends on Redis::Namespace, #connection_for will perform
87
115
  # special Redis::Namespace handling for a connection with the name
88
116
  # +:resque+.
89
117
  #
90
- # If #narrow? is true, the same Redis connection will be shared between all
91
- # clients.
92
- #
93
- # If a connection has not yet been made, it will be made.
94
- def connection_for(client_name)
95
- connect unless connection
96
- return nil if client_name == :all
97
- @clients[client_name] ||= case client_name
98
- when :resque
99
- connect_for_resque
100
- else
101
- connect_for_any
102
- end
103
- end
118
+ # :method: connection_for
104
119
 
120
+ ##
105
121
  # Reconnect to Redis for some or all clients. The primary connection will
106
122
  # always be reconnected; other clients will be reconnected based on the
107
123
  # +clients+ provided. Only clients actively managed by previous calls to
108
124
  # #connect or #connection_for will be reconnected.
109
125
  #
110
126
  # If #reconnect is called with the value +:all+, all currently managed
111
- # clients will be reconnected.
127
+ # clients will be reconnected. If #narrow? is true, the primary connection
128
+ # will be reconnected.
112
129
  #
113
- # If #narrow? is true, only the primary connection will be reconnected.
114
- def reconnect(*client_names)
115
- return unless connection
116
-
117
- connection.client.reconnect
118
-
119
- unless narrow?
120
- clients_from(*client_names).each { |client_name|
121
- redis = @clients[client_name]
122
- redis.client.reconnect if redis
123
- }
124
- end
125
-
126
- connection
127
- end
130
+ # :method: reconnect
128
131
 
132
+ ##
129
133
  # Disconnect from Redis for some or all clients. The primary connection
130
134
  # will always be disconnected; other clients will be disconnected based on
131
135
  # the +clients+ provided. Only clients actively managed by previous calls
132
136
  # to #connect or #connection_for will be disconnected.
133
137
  #
134
138
  # If #disconnect is called with the value +:all+, all currently managed
135
- # clients will be disconnected.
139
+ # clients will be disconnected. If #narrow? is true, the primary connection
140
+ # will be disconnected.
136
141
  #
137
- # If #narrow? is true, only the primary connection will be disconnected.
138
- def disconnect(*client_names)
139
- return unless connection
140
-
141
- unless narrow?
142
- clients_from(*client_names).each { |client_name|
143
- redis = @clients[client_name]
144
- redis.quit if redis
145
- }
146
- end
147
-
148
- connection.quit
149
- end
142
+ # :method: disconnect
150
143
 
151
144
  private
152
- def clients_from(*client_names)
153
- if client_names.size == 1
154
- if client_names.first == :all
155
- @clients.keys
156
- else
157
- client_names
158
- end
145
+ def client_connect(name = nil, options = {})
146
+ options = { namespace: options[:namespace] }
147
+
148
+ case name
149
+ when :resque
150
+ connect_for_resque(options)
159
151
  else
160
- client_names
152
+ connect_for_any(options)
161
153
  end
162
154
  end
163
155
 
164
- def connect_for_any
165
- return connection if connection && narrow?
156
+ def client_reconnect(redis = connection())
157
+ redis.client.reconnect if redis
158
+ end
166
159
 
167
- r = ::Redis.new(@redis_options)
168
- if @namespace
169
- r = ::Redis::Namespace.new(@namespace, redis: r)
160
+ def client_disconnect(redis = connection())
161
+ redis.quit if redis
162
+ end
163
+
164
+ def connect_for_any(options)
165
+ r = if connection && narrow?
166
+ connection
167
+ else
168
+ r = ::Redis.new(@redis_options.merge(options))
169
+
170
+ if @namespace
171
+ ::Redis::Namespace.new(@namespace, redis: r)
172
+ else
173
+ r
174
+ end
175
+ end
176
+
177
+ if options[:namespace]
178
+ r = ::Redis::Namespace.new(options[:namespace], redis: r)
170
179
  end
180
+
171
181
  r
172
182
  end
173
183
 
174
- def connect_for_resque
175
- r = connect_for_any
184
+ def connect_for_resque(options)
185
+ r = connect_for_any(options)
176
186
 
177
187
  if r.instance_of?(::Redis::Namespace) && r.namespace.to_s !~ /:resque\z/
178
188
  r = ::Redis::Namespace.new(:"#{r.namespace}:resque", redis: r.redis)
@@ -105,13 +105,17 @@ describe Stockpile::Redis do
105
105
 
106
106
  describe "with a wide connection width" do
107
107
  before do
108
- rcm_wide.connect(:redis, :rollout)
108
+ rcm_wide.connect(:redis, rollout: { namespace: 'rollout' })
109
109
  end
110
110
 
111
111
  it "connects multiple clients" do
112
112
  assert_clients [ :redis, :rollout ], rcm_wide
113
113
  end
114
114
 
115
+ it "creates a Redis::Namespace for rollout" do
116
+ assert_kind_of Redis::Namespace, rcm_wide.connection_for(:rollout)
117
+ end
118
+
115
119
  it "connects *different* clients" do
116
120
  refute_same rcm_wide.connection, rcm_wide.connection_for(:redis)
117
121
  refute_same rcm_wide.connection, rcm_wide.connection_for(:rollout)
@@ -121,17 +125,25 @@ describe Stockpile::Redis do
121
125
 
122
126
  describe "with a narrow connection width" do
123
127
  before do
124
- rcm_narrow.connect(:redis, :rollout)
128
+ rcm_narrow.connect(:redis, rollout: { namespace: 'rollout' })
125
129
  end
126
130
 
127
131
  it "appears to connect multiple clients" do
128
132
  assert_clients [ :redis, :rollout ], rcm_narrow
129
133
  end
130
134
 
135
+ it "creates a Redis::Namespace for rollout" do
136
+ assert_kind_of Redis::Namespace, rcm_narrow.connection_for(:rollout)
137
+ end
138
+
131
139
  it "returns identical clients" do
132
140
  assert_same rcm_narrow.connection, rcm_narrow.connection_for(:redis)
133
- assert_same rcm_narrow.connection, rcm_narrow.connection_for(:rollout)
134
- assert_same rcm_narrow.connection_for(:redis), rcm_narrow.connection_for(:rollout)
141
+ refute_same rcm_narrow.connection, rcm_narrow.connection_for(:rollout)
142
+ assert_same rcm_narrow.connection,
143
+ rcm_narrow.connection_for(:rollout).redis
144
+ refute_same rcm_narrow.connection_for(:redis), rcm_narrow.connection_for(:rollout)
145
+ assert_same rcm_narrow.connection_for(:redis),
146
+ rcm_narrow.connection_for(:rollout).redis
135
147
  end
136
148
  end
137
149
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stockpile-redis
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Ziegler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-21 00:00:00.000000000 Z
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stockpile
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -294,7 +294,6 @@ files:
294
294
  - ".gemtest"
295
295
  - ".minitest.rb"
296
296
  - ".travis.yml"
297
- - ".workenv"
298
297
  - Contributing.rdoc
299
298
  - Gemfile
300
299
  - History.rdoc
@@ -333,6 +332,4 @@ rubygems_version: 2.2.2
333
332
  signing_key:
334
333
  specification_version: 4
335
334
  summary: stockpile-redis is a connection manager for Redis to be used with {Stockpile}[https://github.com/halostatue/stockpile].
336
- test_files:
337
- - test/test_stockpile_adapter_redis.rb
338
- - test/test_stockpile_redis.rb
335
+ test_files: []
data/.workenv DELETED
@@ -1,5 +0,0 @@
1
- #! zsh
2
-
3
- --workenv-tab --first
4
- --workenv-tab --desc autotest autotest
5
- --workenv-tab --desc documents "fswatch -o *.rdoc lib/**/*.rb| xargs -n1 -I{} rake docs"