stockpile-redis 1.0 → 1.1
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 +4 -4
- data/History.rdoc +10 -0
- data/Manifest.txt +0 -1
- data/README.rdoc +11 -2
- data/Rakefile +1 -1
- data/lib/stockpile/redis.rb +105 -95
- data/test/test_stockpile_redis.rb +16 -4
- metadata +5 -8
- data/.workenv +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c921c5dea37bc0ab0c8f2b5ebb77bb3db14071a3
|
4
|
+
data.tar.gz: 75fa3e0437603717c027d83270d36f93eb814852
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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.
|
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.
|
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']
|
data/lib/stockpile/redis.rb
CHANGED
@@ -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.
|
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
|
-
|
32
|
-
|
33
|
-
@
|
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
|
-
|
42
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
81
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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
|
-
|
152
|
+
connect_for_any(options)
|
161
153
|
end
|
162
154
|
end
|
163
155
|
|
164
|
-
def
|
165
|
-
|
156
|
+
def client_reconnect(redis = connection())
|
157
|
+
redis.client.reconnect if redis
|
158
|
+
end
|
166
159
|
|
167
|
-
|
168
|
-
if
|
169
|
-
|
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
|
-
|
134
|
-
assert_same rcm_narrow.
|
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.
|
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-
|
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.
|
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.
|
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: []
|