redis-namespace 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of redis-namespace might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Rakefile +11 -5
- data/lib/redis/namespace.rb +65 -9
- data/spec/redis_spec.rb +182 -82
- data/spec/spec_helper.rb +14 -0
- metadata +10 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e20a131134003e74cb1b40bcf3ad11c61dae9d9d
|
4
|
+
data.tar.gz: f7718c7e9ea9ce7e8a46889c78bebe9a7d4a09ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00d8dca8c63810e4029cfc5430bb1507f9a1736320633ab703b049fc249c35160d1018151e7f6825f0eba960e868b82e7bd1836c6130fe850941b2825d5dfafd
|
7
|
+
data.tar.gz: 0fd499ddcf3c019525f6393eb4ba5957ebee559727924ea3c827e20cd57f818cca7e246ace91cea209b3914c7c0dde57e57e334aeab1af4d3e26b88fa7a1a38e
|
data/Rakefile
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
require "bundler/gem_tasks"
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.pattern = 'spec/*_spec.rb'
|
8
|
+
spec.rspec_opts = ['--backtrace']
|
9
|
+
spec.ruby_opts = ['-w']
|
7
10
|
end
|
11
|
+
|
12
|
+
task :default => :spec
|
13
|
+
task :test => :spec
|
data/lib/redis/namespace.rb
CHANGED
@@ -64,6 +64,8 @@ class Redis
|
|
64
64
|
"exists" => [ :first ],
|
65
65
|
"expire" => [ :first ],
|
66
66
|
"expireat" => [ :first ],
|
67
|
+
"eval" => [ :eval_style ],
|
68
|
+
"evalsha" => [ :eval_style ],
|
67
69
|
"flushall" => [],
|
68
70
|
"flushdb" => [],
|
69
71
|
"get" => [ :first ],
|
@@ -102,6 +104,8 @@ class Redis
|
|
102
104
|
"mapped_hmset" => [ :first ],
|
103
105
|
"mapped_hmget" => [ :first ],
|
104
106
|
"mapped_mget" => [ :all, :all ],
|
107
|
+
"mapped_mset" => [ :all ],
|
108
|
+
"mapped_msetnx" => [ :all ],
|
105
109
|
"mget" => [ :all ],
|
106
110
|
"monitor" => [ :monitor ],
|
107
111
|
"move" => [ :first ],
|
@@ -182,12 +186,14 @@ class Redis
|
|
182
186
|
else {}
|
183
187
|
end
|
184
188
|
|
185
|
-
|
189
|
+
attr_writer :namespace
|
186
190
|
attr_reader :redis
|
191
|
+
attr_accessor :warning
|
187
192
|
|
188
193
|
def initialize(namespace, options = {})
|
189
194
|
@namespace = namespace
|
190
195
|
@redis = options[:redis] || Redis.current
|
196
|
+
@warning = options[:warning] || false
|
191
197
|
end
|
192
198
|
|
193
199
|
# Ruby defines a now deprecated type method so we need to override it here
|
@@ -210,8 +216,21 @@ class Redis
|
|
210
216
|
query.nil? ? super("*") : super
|
211
217
|
end
|
212
218
|
|
213
|
-
def
|
214
|
-
|
219
|
+
def multi(&block)
|
220
|
+
namespaced_block(:multi, &block)
|
221
|
+
end
|
222
|
+
|
223
|
+
def pipelined(&block)
|
224
|
+
namespaced_block(:pipelined, &block)
|
225
|
+
end
|
226
|
+
|
227
|
+
def namespace(desired_namespace = nil)
|
228
|
+
if desired_namespace
|
229
|
+
yield Redis::Namespace.new(desired_namespace,
|
230
|
+
:redis => @redis)
|
231
|
+
end
|
232
|
+
|
233
|
+
@namespace
|
215
234
|
end
|
216
235
|
|
217
236
|
def method_missing(command, *args, &block)
|
@@ -219,8 +238,10 @@ class Redis
|
|
219
238
|
COMMANDS[ALIASES[command.to_s]]
|
220
239
|
|
221
240
|
# redis-namespace does not know how to handle this command.
|
222
|
-
# Passing it to @redis as is
|
241
|
+
# Passing it to @redis as is, where redis-namespace shows
|
242
|
+
# a warning message if @warning is set.
|
223
243
|
if handling.nil?
|
244
|
+
warn("Passing '#{command}' command to redis as is.") if @warning
|
224
245
|
return @redis.send(command, *args, &block)
|
225
246
|
end
|
226
247
|
|
@@ -237,7 +258,7 @@ class Redis
|
|
237
258
|
args = add_namespace(args)
|
238
259
|
args.unshift(first) if first
|
239
260
|
when :exclude_last
|
240
|
-
last = args.pop
|
261
|
+
last = args.pop unless args.length == 1
|
241
262
|
args = add_namespace(args)
|
242
263
|
args.push(last) if last
|
243
264
|
when :exclude_options
|
@@ -252,9 +273,33 @@ class Redis
|
|
252
273
|
args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
|
253
274
|
when :sort
|
254
275
|
args[0] = add_namespace(args[0]) if args[0]
|
255
|
-
[
|
256
|
-
|
257
|
-
|
276
|
+
if args[1].is_a?(Hash)
|
277
|
+
[:by, :store].each do |key|
|
278
|
+
args[1][key] = add_namespace(args[1][key]) if args[1][key]
|
279
|
+
end
|
280
|
+
|
281
|
+
args[1][:get] = Array(args[1][:get])
|
282
|
+
|
283
|
+
args[1][:get].each_index do |i|
|
284
|
+
args[1][:get][i] = add_namespace(args[1][:get][i]) unless args[1][:get][i] == "#"
|
285
|
+
end
|
286
|
+
end
|
287
|
+
when :eval_style
|
288
|
+
# redis.eval() and evalsha() can either take the form:
|
289
|
+
#
|
290
|
+
# redis.eval(script, [key1, key2], [argv1, argv2])
|
291
|
+
#
|
292
|
+
# Or:
|
293
|
+
#
|
294
|
+
# redis.eval(script, :keys => ['k1', 'k2'], :argv => ['arg1', 'arg2'])
|
295
|
+
#
|
296
|
+
# This is a tricky + annoying special case, where we only want the `keys`
|
297
|
+
# argument to be namespaced.
|
298
|
+
if args.last.is_a?(Hash)
|
299
|
+
args.last[:keys] = add_namespace(args.last[:keys])
|
300
|
+
else
|
301
|
+
args[1] = add_namespace(args[1])
|
302
|
+
end
|
258
303
|
end
|
259
304
|
|
260
305
|
# Dispatch the command to Redis and store the result.
|
@@ -272,6 +317,17 @@ class Redis
|
|
272
317
|
end
|
273
318
|
|
274
319
|
private
|
320
|
+
|
321
|
+
def namespaced_block(command, &block)
|
322
|
+
original = @redis
|
323
|
+
result = redis.send(command) do |r|
|
324
|
+
@redis = r
|
325
|
+
yield self
|
326
|
+
end
|
327
|
+
@redis = original
|
328
|
+
result
|
329
|
+
end
|
330
|
+
|
275
331
|
def add_namespace(key)
|
276
332
|
return key unless key && @namespace
|
277
333
|
|
@@ -294,7 +350,7 @@ class Redis
|
|
294
350
|
when Hash
|
295
351
|
Hash[*key.map {|k, v| [ rem_namespace(k), v ]}.flatten]
|
296
352
|
else
|
297
|
-
key.to_s.gsub
|
353
|
+
key.to_s.gsub(/^#{@namespace}:/, "")
|
298
354
|
end
|
299
355
|
end
|
300
356
|
end
|
data/spec/redis_spec.rb
CHANGED
@@ -23,28 +23,30 @@ describe "redis" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "proxies `client` to the client" do
|
26
|
-
@namespaced.client.should
|
26
|
+
@namespaced.client.should eq(@redis.client)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should be able to use a namespace" do
|
30
|
-
@namespaced['foo'].should
|
30
|
+
@namespaced['foo'].should eq(nil)
|
31
31
|
@namespaced['foo'] = 'chris'
|
32
|
-
@namespaced['foo'].should
|
32
|
+
@namespaced['foo'].should eq('chris')
|
33
33
|
@redis['foo'] = 'bob'
|
34
|
-
@redis['foo'].should
|
34
|
+
@redis['foo'].should eq('bob')
|
35
35
|
|
36
36
|
@namespaced.incrby('counter', 2)
|
37
|
-
@namespaced['counter'].to_i.should
|
38
|
-
@redis['counter'].should
|
39
|
-
@namespaced.type('counter').should
|
37
|
+
@namespaced['counter'].to_i.should eq(2)
|
38
|
+
@redis['counter'].should eq(nil)
|
39
|
+
@namespaced.type('counter').should eq('string')
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should be able to use a namespace with bpop" do
|
43
43
|
@namespaced.rpush "foo", "string"
|
44
44
|
@namespaced.rpush "foo", "ns:string"
|
45
|
-
@namespaced.
|
46
|
-
@namespaced.blpop("foo", 1).should
|
47
|
-
@namespaced.blpop("foo", 1).should
|
45
|
+
@namespaced.rpush "foo", "string_no_timeout"
|
46
|
+
@namespaced.blpop("foo", 1).should eq(["foo", "string"])
|
47
|
+
@namespaced.blpop("foo", 1).should eq(["foo", "ns:string"])
|
48
|
+
@namespaced.blpop("foo").should eq(["foo", "string_no_timeout"])
|
49
|
+
@namespaced.blpop("foo", 1).should eq(nil)
|
48
50
|
end
|
49
51
|
|
50
52
|
it "should be able to use a namespace with del" do
|
@@ -52,114 +54,126 @@ describe "redis" do
|
|
52
54
|
@namespaced['bar'] = 2000
|
53
55
|
@namespaced['baz'] = 3000
|
54
56
|
@namespaced.del 'foo'
|
55
|
-
@namespaced['foo'].should
|
57
|
+
@namespaced['foo'].should eq(nil)
|
56
58
|
@namespaced.del 'bar', 'baz'
|
57
|
-
@namespaced['bar'].should
|
58
|
-
@namespaced['baz'].should
|
59
|
+
@namespaced['bar'].should eq(nil)
|
60
|
+
@namespaced['baz'].should eq(nil)
|
59
61
|
end
|
60
62
|
|
61
63
|
it 'should be able to use a namespace with append' do
|
62
64
|
@namespaced['foo'] = 'bar'
|
63
|
-
@namespaced.append('foo','n').should
|
64
|
-
@namespaced['foo'].should
|
65
|
-
@redis['foo'].should
|
65
|
+
@namespaced.append('foo','n').should eq(4)
|
66
|
+
@namespaced['foo'].should eq('barn')
|
67
|
+
@redis['foo'].should eq('bar')
|
66
68
|
end
|
67
69
|
|
68
70
|
it 'should be able to use a namespace with brpoplpush' do
|
69
71
|
@namespaced.lpush('foo','bar')
|
70
|
-
@namespaced.brpoplpush('foo','bar',0).should
|
71
|
-
@namespaced.lrange('foo',0,-1).should
|
72
|
-
@namespaced.lrange('bar',0,-1).should
|
72
|
+
@namespaced.brpoplpush('foo','bar',0).should eq('bar')
|
73
|
+
@namespaced.lrange('foo',0,-1).should eq([])
|
74
|
+
@namespaced.lrange('bar',0,-1).should eq(['bar'])
|
73
75
|
end
|
74
76
|
|
75
77
|
it 'should be able to use a namespace with getbit' do
|
76
78
|
@namespaced.set('foo','bar')
|
77
|
-
@namespaced.getbit('foo',1).should
|
79
|
+
@namespaced.getbit('foo',1).should eq(1)
|
78
80
|
end
|
79
81
|
|
80
82
|
it 'should be able to use a namespace with getrange' do
|
81
83
|
@namespaced.set('foo','bar')
|
82
|
-
@namespaced.getrange('foo',0,-1).should
|
84
|
+
@namespaced.getrange('foo',0,-1).should eq('bar')
|
83
85
|
end
|
84
86
|
|
85
87
|
it 'should be able to use a namespace with linsert' do
|
86
88
|
@namespaced.rpush('foo','bar')
|
87
89
|
@namespaced.rpush('foo','barn')
|
88
90
|
@namespaced.rpush('foo','bart')
|
89
|
-
@namespaced.linsert('foo','BEFORE','barn','barf').should
|
90
|
-
@namespaced.lrange('foo',0,-1).should
|
91
|
+
@namespaced.linsert('foo','BEFORE','barn','barf').should eq(4)
|
92
|
+
@namespaced.lrange('foo',0,-1).should eq(['bar','barf','barn','bart'])
|
91
93
|
end
|
92
94
|
|
93
95
|
it 'should be able to use a namespace with lpushx' do
|
94
|
-
@namespaced.lpushx('foo','bar').should
|
96
|
+
@namespaced.lpushx('foo','bar').should eq(0)
|
95
97
|
@namespaced.lpush('foo','boo')
|
96
|
-
@namespaced.lpushx('foo','bar').should
|
97
|
-
@namespaced.lrange('foo',0,-1).should
|
98
|
+
@namespaced.lpushx('foo','bar').should eq(2)
|
99
|
+
@namespaced.lrange('foo',0,-1).should eq(['bar','boo'])
|
98
100
|
end
|
99
101
|
|
100
102
|
it 'should be able to use a namespace with rpushx' do
|
101
|
-
@namespaced.rpushx('foo','bar').should
|
103
|
+
@namespaced.rpushx('foo','bar').should eq(0)
|
102
104
|
@namespaced.lpush('foo','boo')
|
103
|
-
@namespaced.rpushx('foo','bar').should
|
104
|
-
@namespaced.lrange('foo',0,-1).should
|
105
|
+
@namespaced.rpushx('foo','bar').should eq(2)
|
106
|
+
@namespaced.lrange('foo',0,-1).should eq(['boo','bar'])
|
105
107
|
end
|
106
108
|
|
107
109
|
it 'should be able to use a namespace with setbit' do
|
108
110
|
@namespaced.setbit('virgin_key', 1, 1)
|
109
111
|
@namespaced.exists('virgin_key').should be_true
|
110
|
-
@namespaced.get('virgin_key').should
|
112
|
+
@namespaced.get('virgin_key').should eq(@namespaced.getrange('virgin_key',0,-1))
|
111
113
|
end
|
112
114
|
|
113
115
|
it 'should be able to use a namespace with setrange' do
|
114
116
|
@namespaced.setrange('foo', 0, 'bar')
|
115
|
-
@namespaced['foo'].should
|
117
|
+
@namespaced['foo'].should eq('bar')
|
116
118
|
|
117
119
|
@namespaced.setrange('bar', 2, 'foo')
|
118
|
-
@namespaced['bar'].should
|
120
|
+
@namespaced['bar'].should eq("\000\000foo")
|
119
121
|
end
|
120
122
|
|
121
123
|
it "should be able to use a namespace with mget" do
|
122
124
|
@namespaced['foo'] = 1000
|
123
125
|
@namespaced['bar'] = 2000
|
124
|
-
@namespaced.mapped_mget('foo', 'bar').should
|
125
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').should
|
126
|
+
@namespaced.mapped_mget('foo', 'bar').should eq({ 'foo' => '1000', 'bar' => '2000' })
|
127
|
+
@namespaced.mapped_mget('foo', 'baz', 'bar').should eq({'foo'=>'1000', 'bar'=>'2000', 'baz' => nil})
|
126
128
|
end
|
127
129
|
|
128
130
|
it "should be able to use a namespace with mset" do
|
129
131
|
@namespaced.mset('foo', '1000', 'bar', '2000')
|
130
|
-
@namespaced.mapped_mget('foo', 'bar').should
|
131
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').should
|
132
|
+
@namespaced.mapped_mget('foo', 'bar').should eq({ 'foo' => '1000', 'bar' => '2000' })
|
133
|
+
@namespaced.mapped_mget('foo', 'baz', 'bar').should eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
|
134
|
+
@namespaced.mapped_mset('foo' => '3000', 'bar' => '5000')
|
135
|
+
@namespaced.mapped_mget('foo', 'bar').should eq({ 'foo' => '3000', 'bar' => '5000' })
|
136
|
+
@namespaced.mapped_mget('foo', 'baz', 'bar').should eq({ 'foo' => '3000', 'bar' => '5000', 'baz' => nil})
|
132
137
|
end
|
133
138
|
|
134
139
|
it "should be able to use a namespace with msetnx" do
|
135
140
|
@namespaced.msetnx('foo', '1000', 'bar', '2000')
|
136
|
-
@namespaced.mapped_mget('foo', 'bar').should
|
137
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').should
|
141
|
+
@namespaced.mapped_mget('foo', 'bar').should eq({ 'foo' => '1000', 'bar' => '2000' })
|
142
|
+
@namespaced.mapped_mget('foo', 'baz', 'bar').should eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should be able to use a namespace with mapped_msetnx" do
|
146
|
+
puts @namespaced.keys.inspect
|
147
|
+
@namespaced.set('foo','1')
|
148
|
+
@namespaced.mapped_msetnx('foo'=>'1000', 'bar'=>'2000').should be_false
|
149
|
+
@namespaced.mapped_mget('foo', 'bar').should == { 'foo' => '1', 'bar' => nil }
|
150
|
+
@namespaced.mapped_msetnx('bar'=>'2000', 'baz'=>'1000').should be_true
|
151
|
+
@namespaced.mapped_mget('foo', 'bar').should == { 'foo' => '1', 'bar' => '2000' }
|
138
152
|
end
|
139
153
|
|
140
154
|
it "should be able to use a namespace with hashes" do
|
141
155
|
@namespaced.hset('foo', 'key', 'value')
|
142
156
|
@namespaced.hset('foo', 'key1', 'value1')
|
143
|
-
@namespaced.hget('foo', 'key').should
|
144
|
-
@namespaced.hgetall('foo').should
|
145
|
-
@namespaced.hlen('foo').should
|
146
|
-
@namespaced.hkeys('foo').should
|
157
|
+
@namespaced.hget('foo', 'key').should eq('value')
|
158
|
+
@namespaced.hgetall('foo').should eq({'key' => 'value', 'key1' => 'value1'})
|
159
|
+
@namespaced.hlen('foo').should eq(2)
|
160
|
+
@namespaced.hkeys('foo').should eq(['key', 'key1'])
|
147
161
|
@namespaced.hmset('bar', 'key', 'value', 'key1', 'value1')
|
148
162
|
@namespaced.hmget('bar', 'key', 'key1')
|
149
163
|
@namespaced.hmset('bar', 'a_number', 1)
|
150
|
-
@namespaced.hmget('bar', 'a_number').should
|
164
|
+
@namespaced.hmget('bar', 'a_number').should eq(['1'])
|
151
165
|
@namespaced.hincrby('bar', 'a_number', 3)
|
152
|
-
@namespaced.hmget('bar', 'a_number').should
|
153
|
-
@namespaced.hgetall('bar').should
|
166
|
+
@namespaced.hmget('bar', 'a_number').should eq(['4'])
|
167
|
+
@namespaced.hgetall('bar').should eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
154
168
|
|
155
169
|
@namespaced.hsetnx('foonx','nx',10).should be_true
|
156
170
|
@namespaced.hsetnx('foonx','nx',12).should be_false
|
157
|
-
@namespaced.hget('foonx','nx').should
|
158
|
-
@namespaced.hkeys('foonx').should
|
159
|
-
@namespaced.hvals('foonx').should
|
171
|
+
@namespaced.hget('foonx','nx').should eq("10")
|
172
|
+
@namespaced.hkeys('foonx').should eq(%w{ nx })
|
173
|
+
@namespaced.hvals('foonx').should eq(%w{ 10 })
|
160
174
|
@namespaced.mapped_hmset('baz', {'key' => 'value', 'key1' => 'value1', 'a_number' => 4})
|
161
|
-
@namespaced.mapped_hmget('baz', 'key', 'key1', 'a_number').should
|
162
|
-
@namespaced.hgetall('baz').should
|
175
|
+
@namespaced.mapped_hmget('baz', 'key', 'key1', 'a_number').should eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
176
|
+
@namespaced.hgetall('baz').should eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
163
177
|
end
|
164
178
|
|
165
179
|
it "should properly intersect three sets" do
|
@@ -170,7 +184,7 @@ describe "redis" do
|
|
170
184
|
@namespaced.sadd('bar', 3)
|
171
185
|
@namespaced.sadd('bar', 4)
|
172
186
|
@namespaced.sadd('baz', 3)
|
173
|
-
@namespaced.sinter('foo', 'bar', 'baz').should
|
187
|
+
@namespaced.sinter('foo', 'bar', 'baz').should eq(%w( 3 ))
|
174
188
|
end
|
175
189
|
|
176
190
|
it "should properly union two sets" do
|
@@ -179,7 +193,7 @@ describe "redis" do
|
|
179
193
|
@namespaced.sadd('bar', 2)
|
180
194
|
@namespaced.sadd('bar', 3)
|
181
195
|
@namespaced.sadd('bar', 4)
|
182
|
-
@namespaced.sunion('foo', 'bar').sort.should
|
196
|
+
@namespaced.sunion('foo', 'bar').sort.should eq(%w( 1 2 3 4 ))
|
183
197
|
end
|
184
198
|
|
185
199
|
it "should properly union two sorted sets with options" do
|
@@ -189,7 +203,7 @@ describe "redis" do
|
|
189
203
|
@namespaced.zadd('sort2', 3, 3)
|
190
204
|
@namespaced.zadd('sort2', 4, 4)
|
191
205
|
@namespaced.zunionstore('union', ['sort1', 'sort2'], :weights => [2, 1])
|
192
|
-
@namespaced.zrevrange('union', 0, -1).should
|
206
|
+
@namespaced.zrevrange('union', 0, -1).should eq(%w( 2 4 3 1 ))
|
193
207
|
end
|
194
208
|
|
195
209
|
it "should properly union two sorted sets without options" do
|
@@ -199,7 +213,7 @@ describe "redis" do
|
|
199
213
|
@namespaced.zadd('sort2', 3, 3)
|
200
214
|
@namespaced.zadd('sort2', 4, 4)
|
201
215
|
@namespaced.zunionstore('union', ['sort1', 'sort2'])
|
202
|
-
@namespaced.zrevrange('union', 0, -1).should
|
216
|
+
@namespaced.zrevrange('union', 0, -1).should eq(%w( 4 2 3 1 ))
|
203
217
|
end
|
204
218
|
|
205
219
|
it "should add namespace to sort" do
|
@@ -210,43 +224,99 @@ describe "redis" do
|
|
210
224
|
@namespaced.set('value_1', 'a')
|
211
225
|
@namespaced.set('value_2', 'b')
|
212
226
|
|
213
|
-
@namespaced.sort('foo').should
|
214
|
-
@namespaced.sort('foo', :limit => [0, 1]).should
|
215
|
-
@namespaced.sort('foo', :order => 'desc').should
|
216
|
-
@namespaced.sort('foo', :by => 'weight_*').should
|
217
|
-
@namespaced.sort('foo', :get => 'value_*').should
|
227
|
+
@namespaced.sort('foo').should eq(%w( 1 2 ))
|
228
|
+
@namespaced.sort('foo', :limit => [0, 1]).should eq(%w( 1 ))
|
229
|
+
@namespaced.sort('foo', :order => 'desc').should eq(%w( 2 1 ))
|
230
|
+
@namespaced.sort('foo', :by => 'weight_*').should eq(%w( 2 1 ))
|
231
|
+
@namespaced.sort('foo', :get => 'value_*').should eq(%w( a b ))
|
232
|
+
@namespaced.sort('foo', :get => '#').should eq(%w( 1 2 ))
|
233
|
+
@namespaced.sort('foo', :get => ['#', 'value_*']).should eq([["1", "a"], ["2", "b"]])
|
218
234
|
|
219
235
|
@namespaced.sort('foo', :store => 'result')
|
220
|
-
@namespaced.lrange('result', 0, -1).should
|
236
|
+
@namespaced.lrange('result', 0, -1).should eq(%w( 1 2 ))
|
221
237
|
end
|
222
238
|
|
223
239
|
it "should yield the correct list of keys" do
|
224
240
|
@namespaced["foo"] = 1
|
225
241
|
@namespaced["bar"] = 2
|
226
242
|
@namespaced["baz"] = 3
|
227
|
-
@namespaced.keys("*").sort.should
|
228
|
-
@namespaced.keys.sort.should
|
243
|
+
@namespaced.keys("*").sort.should eq(%w( bar baz foo ))
|
244
|
+
@namespaced.keys.sort.should eq(%w( bar baz foo ))
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should add namepsace to multi blocks" do
|
248
|
+
@namespaced.mapped_hmset "foo", {"key" => "value"}
|
249
|
+
@namespaced.multi do |r|
|
250
|
+
r.del "foo"
|
251
|
+
r.mapped_hmset "foo", {"key1" => "value1"}
|
252
|
+
end
|
253
|
+
@namespaced.hgetall("foo").should eq({"key1" => "value1"})
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should add namespace to pipelined blocks" do
|
257
|
+
@namespaced.mapped_hmset "foo", {"key" => "value"}
|
258
|
+
@namespaced.pipelined do |r|
|
259
|
+
r.del "foo"
|
260
|
+
r.mapped_hmset "foo", {"key1" => "value1"}
|
261
|
+
end
|
262
|
+
@namespaced.hgetall("foo").should eq({"key1" => "value1"})
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should returned response array from pipelined block" do
|
266
|
+
@namespaced.mset "foo", "bar", "key", "value"
|
267
|
+
result = @namespaced.pipelined do |r|
|
268
|
+
r["foo"]
|
269
|
+
r["key"]
|
270
|
+
end
|
271
|
+
result.should eq(["bar", "value"])
|
229
272
|
end
|
230
273
|
|
231
274
|
it "can change its namespace" do
|
232
|
-
@namespaced['foo'].should
|
275
|
+
@namespaced['foo'].should eq(nil)
|
233
276
|
@namespaced['foo'] = 'chris'
|
234
|
-
@namespaced['foo'].should
|
277
|
+
@namespaced['foo'].should eq('chris')
|
235
278
|
|
236
|
-
@namespaced.namespace.should
|
279
|
+
@namespaced.namespace.should eq(:ns)
|
237
280
|
@namespaced.namespace = :spec
|
238
|
-
@namespaced.namespace.should
|
281
|
+
@namespaced.namespace.should eq(:spec)
|
239
282
|
|
240
|
-
@namespaced['foo'].should
|
283
|
+
@namespaced['foo'].should eq(nil)
|
241
284
|
@namespaced['foo'] = 'chris'
|
242
|
-
@namespaced['foo'].should
|
285
|
+
@namespaced['foo'].should eq('chris')
|
286
|
+
end
|
287
|
+
|
288
|
+
it "can accept a temporary namespace" do
|
289
|
+
@namespaced.namespace.should eq(:ns)
|
290
|
+
@namespaced['foo'].should eq(nil)
|
291
|
+
|
292
|
+
@namespaced.namespace(:spec) do |temp_ns|
|
293
|
+
temp_ns.namespace.should eq(:spec)
|
294
|
+
temp_ns['foo'].should eq(nil)
|
295
|
+
temp_ns['foo'] = 'jake'
|
296
|
+
temp_ns['foo'].should eq('jake')
|
297
|
+
end
|
298
|
+
|
299
|
+
@namespaced.namespace.should eq(:ns)
|
300
|
+
@namespaced['foo'].should eq(nil)
|
243
301
|
end
|
244
302
|
|
245
303
|
it "should respond to :namespace=" do
|
246
|
-
@namespaced.respond_to?(:namespace=).should
|
304
|
+
@namespaced.respond_to?(:namespace=).should eq(true)
|
305
|
+
end
|
306
|
+
|
307
|
+
it "should respond to :warning=" do
|
308
|
+
@namespaced.respond_to?(:warning=).should == true
|
309
|
+
end
|
310
|
+
|
311
|
+
it "should warn against unknown commands if :warning is true" do
|
312
|
+
@namespaced.warning = true
|
313
|
+
capture_stderr {
|
314
|
+
@namespaced.unknown('foo')
|
315
|
+
}.should == "Passing 'unknown' command to redis as is."
|
247
316
|
end
|
248
317
|
|
249
|
-
|
318
|
+
# Redis 2.6 RC reports its version as 2.5.
|
319
|
+
if @redis_version >= Gem::Version.new("2.5.0")
|
250
320
|
describe "redis 2.6 commands" do
|
251
321
|
it "should namespace bitcount" do
|
252
322
|
pending "awaiting implementaton of command in redis gem"
|
@@ -262,50 +332,80 @@ describe "redis" do
|
|
262
332
|
|
263
333
|
it "should namespace hincrbyfloat" do
|
264
334
|
@namespaced.hset('mykey', 'field', 10.50)
|
265
|
-
@namespaced.hincrbyfloat('mykey', 'field', 0.1).should
|
335
|
+
@namespaced.hincrbyfloat('mykey', 'field', 0.1).should eq(10.6)
|
266
336
|
end
|
267
337
|
|
268
338
|
it "should namespace incrbyfloat" do
|
269
339
|
@namespaced.set('mykey', 10.50)
|
270
|
-
@namespaced.incrbyfloat('mykey', 0.1).should
|
340
|
+
@namespaced.incrbyfloat('mykey', 0.1).should eq(10.6)
|
271
341
|
end
|
272
342
|
|
273
343
|
it "should namespace object" do
|
274
344
|
@namespaced.set('foo', 1000)
|
275
|
-
@namespaced.object('encoding', 'foo').should
|
345
|
+
@namespaced.object('encoding', 'foo').should eq('int')
|
276
346
|
end
|
277
347
|
|
278
348
|
it "should namespace persist" do
|
279
349
|
@namespaced.set('mykey', 'Hello')
|
280
350
|
@namespaced.expire('mykey', 60)
|
281
|
-
@namespaced.persist('mykey').should
|
282
|
-
@namespaced.ttl('mykey').should
|
351
|
+
@namespaced.persist('mykey').should eq(true)
|
352
|
+
@namespaced.ttl('mykey').should eq(-1)
|
283
353
|
end
|
284
354
|
|
285
355
|
it "should namespace pexpire" do
|
286
356
|
@namespaced.set('mykey', 'Hello')
|
287
|
-
@namespaced.pexpire('mykey', 60000).should
|
357
|
+
@namespaced.pexpire('mykey', 60000).should eq(true)
|
288
358
|
end
|
289
359
|
|
290
360
|
it "should namespace pexpireat" do
|
291
361
|
@namespaced.set('mykey', 'Hello')
|
292
|
-
@namespaced.pexpire('mykey', 1555555555005).should
|
362
|
+
@namespaced.pexpire('mykey', 1555555555005).should eq(true)
|
293
363
|
end
|
294
364
|
|
295
365
|
it "should namespace psetex" do
|
296
|
-
@namespaced.psetex('mykey', 10000, 'Hello').should
|
297
|
-
@namespaced.get('mykey').should
|
366
|
+
@namespaced.psetex('mykey', 10000, 'Hello').should eq('OK')
|
367
|
+
@namespaced.get('mykey').should eq('Hello')
|
298
368
|
end
|
299
369
|
|
300
370
|
it "should namespace pttl" do
|
301
371
|
@namespaced.set('mykey', 'Hello')
|
302
372
|
@namespaced.expire('mykey', 1)
|
303
|
-
@namespaced.pttl('mykey').should
|
373
|
+
@namespaced.pttl('mykey').should >= 0
|
304
374
|
end
|
305
375
|
|
306
376
|
it "should namespace restore" do
|
307
377
|
pending "awaiting implementaton of command in redis gem"
|
308
378
|
end
|
379
|
+
|
380
|
+
it "should namespace eval keys passed in as array args" do
|
381
|
+
@namespaced.
|
382
|
+
eval("return {KEYS[1], KEYS[2]}", %w[k1 k2], %w[arg1 arg2]).
|
383
|
+
should eq(%w[ns:k1 ns:k2])
|
384
|
+
end
|
385
|
+
|
386
|
+
it "should namespace eval keys passed in as hash args" do
|
387
|
+
@namespaced.
|
388
|
+
eval("return {KEYS[1], KEYS[2]}", :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
389
|
+
should eq(%w[ns:k1 ns:k2])
|
390
|
+
end
|
391
|
+
|
392
|
+
context '#evalsha' do
|
393
|
+
let!(:sha) do
|
394
|
+
@namespaced.script(:load, "return {KEYS[1], KEYS[2]}")
|
395
|
+
end
|
396
|
+
|
397
|
+
it "should namespace evalsha keys passed in as array args" do
|
398
|
+
@namespaced.
|
399
|
+
evalsha(sha, %w[k1 k2], %w[arg1 arg2]).
|
400
|
+
should eq(%w[ns:k1 ns:k2])
|
401
|
+
end
|
402
|
+
|
403
|
+
it "should namespace evalsha keys passed in as hash args" do
|
404
|
+
@namespaced.
|
405
|
+
evalsha(sha, :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
406
|
+
should eq(%w[ns:k1 ns:k2])
|
407
|
+
end
|
408
|
+
end
|
309
409
|
end
|
310
410
|
end
|
311
411
|
|
@@ -323,12 +423,12 @@ describe "redis" do
|
|
323
423
|
|
324
424
|
it "should support command aliases (push_head)" do
|
325
425
|
@namespaced.push_head('bar', 'quux')
|
326
|
-
@redis.llen('ns:bar').should
|
426
|
+
@redis.llen('ns:bar').should eq(1)
|
327
427
|
end
|
328
428
|
|
329
429
|
it "should support command aliases (zset_add)" do
|
330
430
|
@namespaced.zset_add('bar', 1, 'quux')
|
331
|
-
@redis.zcard('ns:bar').should
|
431
|
+
@redis.zcard('ns:bar').should eq(1)
|
332
432
|
end
|
333
433
|
end
|
334
434
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,6 +11,20 @@ $TESTING=true
|
|
11
11
|
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
12
12
|
require 'redis/namespace'
|
13
13
|
|
14
|
+
def capture_stderr
|
15
|
+
require 'stringio'
|
16
|
+
begin
|
17
|
+
original, $stderr = $stderr, StringIO.new
|
18
|
+
yield
|
19
|
+
rescue Redis::CommandError
|
20
|
+
# ignore Redis::CommandError for test and
|
21
|
+
# return captured messages
|
22
|
+
$stderr.string.chomp
|
23
|
+
ensure
|
24
|
+
$stderr = original
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
14
28
|
RSpec::Matchers.define :have_key do |expected|
|
15
29
|
match do |redis|
|
16
30
|
redis.exists(expected)
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-namespace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wanstrath
|
8
8
|
- Terence Lee
|
9
|
+
- Steve Klabnik
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: redis
|
@@ -57,7 +58,10 @@ description: |
|
|
57
58
|
Adds a Redis::Namespace class which can be used to namespace calls
|
58
59
|
to Redis. This is useful when using a single instance of Redis with
|
59
60
|
multiple, different applications.
|
60
|
-
email:
|
61
|
+
email:
|
62
|
+
- chris@ozmm.org
|
63
|
+
- hone02@gmail.com
|
64
|
+
- steve@steveklabnik.com
|
61
65
|
executables: []
|
62
66
|
extensions: []
|
63
67
|
extra_rdoc_files: []
|
@@ -69,7 +73,7 @@ files:
|
|
69
73
|
- lib/redis-namespace.rb
|
70
74
|
- spec/redis_spec.rb
|
71
75
|
- spec/spec_helper.rb
|
72
|
-
homepage: http://github.com/
|
76
|
+
homepage: http://github.com/resque/redis-namespace
|
73
77
|
licenses: []
|
74
78
|
metadata: {}
|
75
79
|
post_install_message:
|
@@ -88,8 +92,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
92
|
version: '0'
|
89
93
|
requirements: []
|
90
94
|
rubyforge_project:
|
91
|
-
rubygems_version: 2.0.
|
95
|
+
rubygems_version: 2.0.0
|
92
96
|
signing_key:
|
93
97
|
specification_version: 4
|
94
98
|
summary: Namespaces Redis commands.
|
95
99
|
test_files: []
|
100
|
+
has_rdoc: false
|