redis-namespace 1.1.1 → 1.2.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.

data/README.md CHANGED
@@ -35,3 +35,4 @@ Author
35
35
  =====
36
36
 
37
37
  Chris Wanstrath :: chris@ozmm.org
38
+ Terence Lee :: hone02@gmail.com
@@ -25,6 +25,11 @@ class Redis
25
25
  # Add the namespace to all arguments but the last, e.g.
26
26
  # BLPOP key1 key2 timeout =>
27
27
  # BLPOP namespace:key1 namespace:key2 timeout
28
+ # :exclude_options
29
+ # Add the namespace to all arguments, except the last argument,
30
+ # if the last argument is a hash of options.
31
+ # ZUNIONSTORE key1 2 key2 key3 WEIGHTS 2 1 =>
32
+ # ZUNIONSTORE namespace:key1 2 namespace:key2 namespace:key3 WEIGHTS 2 1
28
33
  # :alternate
29
34
  # Add the namespace to every other argument, e.g.
30
35
  # MSET key1 value1 key2 value2 =>
@@ -39,11 +44,14 @@ class Redis
39
44
  # Add the namespace to all elements returned, e.g.
40
45
  # key1 key2 => namespace:key1 namespace:key2
41
46
  COMMANDS = {
47
+ "append" => [:first],
42
48
  "auth" => [],
43
49
  "bgrewriteaof" => [],
44
50
  "bgsave" => [],
45
- "blpop" => [ :exclude_last ],
51
+ "blpop" => [ :exclude_last, :first ],
46
52
  "brpop" => [ :exclude_last ],
53
+ "brpoplpush" => [ :exclude_last ],
54
+ "config" => [],
47
55
  "dbsize" => [],
48
56
  "debug" => [ :exclude_first ],
49
57
  "decr" => [ :first ],
@@ -51,9 +59,12 @@ class Redis
51
59
  "del" => [ :all ],
52
60
  "exists" => [ :first ],
53
61
  "expire" => [ :first ],
62
+ "expireat" => [ :first ],
54
63
  "flushall" => [],
55
64
  "flushdb" => [],
56
65
  "get" => [ :first ],
66
+ "getbit" => [ :first ],
67
+ "getrange" => [ :first ],
57
68
  "getset" => [ :first ],
58
69
  "hset" => [ :first ],
59
70
  "hsetnx" => [ :first ],
@@ -73,20 +84,24 @@ class Redis
73
84
  "keys" => [ :first, :all ],
74
85
  "lastsave" => [],
75
86
  "lindex" => [ :first ],
87
+ "linsert" => [ :first ],
76
88
  "llen" => [ :first ],
77
89
  "lpop" => [ :first ],
78
90
  "lpush" => [ :first ],
91
+ "lpushx" => [ :first ],
79
92
  "lrange" => [ :first ],
80
93
  "lrem" => [ :first ],
81
94
  "lset" => [ :first ],
82
95
  "ltrim" => [ :first ],
83
96
  "mapped_hmset" => [ :first ],
97
+ "mapped_hmget" => [ :first ],
84
98
  "mapped_mget" => [ :all, :all ],
85
99
  "mget" => [ :all ],
86
100
  "monitor" => [ :monitor ],
87
101
  "move" => [ :first ],
88
102
  "mset" => [ :alternate ],
89
103
  "msetnx" => [ :alternate ],
104
+ "ping" => [],
90
105
  "psubscribe" => [ :all ],
91
106
  "publish" => [ :first ],
92
107
  "punsubscribe" => [ :all ],
@@ -97,6 +112,7 @@ class Redis
97
112
  "rpop" => [ :first ],
98
113
  "rpoplpush" => [ :all ],
99
114
  "rpush" => [ :first ],
115
+ "rpushx" => [ :first ],
100
116
  "sadd" => [ :first ],
101
117
  "save" => [],
102
118
  "scard" => [ :first ],
@@ -104,8 +120,10 @@ class Redis
104
120
  "sdiffstore" => [ :all ],
105
121
  "select" => [],
106
122
  "set" => [ :first ],
123
+ "setbit" => [ :first ],
107
124
  "setex" => [ :first ],
108
125
  "setnx" => [ :first ],
126
+ "setrange" => [ :first ],
109
127
  "shutdown" => [],
110
128
  "sinter" => [ :all ],
111
129
  "sinterstore" => [ :all ],
@@ -128,6 +146,7 @@ class Redis
128
146
  "zcard" => [ :first ],
129
147
  "zcount" => [ :first ],
130
148
  "zincrby" => [ :first ],
149
+ "zinterstore" => [ :exclude_options ],
131
150
  "zrange" => [ :first ],
132
151
  "zrangebyscore" => [ :first ],
133
152
  "zrank" => [ :first ],
@@ -135,8 +154,10 @@ class Redis
135
154
  "zremrangebyrank" => [ :first ],
136
155
  "zremrangebyscore" => [ :first ],
137
156
  "zrevrange" => [ :first ],
157
+ "zrevrangebyscore" => [ :first ],
138
158
  "zrevrank" => [ :first ],
139
159
  "zscore" => [ :first ],
160
+ "zunionstore" => [ :exclude_options ],
140
161
  "[]" => [ :first ],
141
162
  "[]=" => [ :first ]
142
163
  }
@@ -152,7 +173,7 @@ class Redis
152
173
 
153
174
  def initialize(namespace, options = {})
154
175
  @namespace = namespace
155
- @redis = options[:redis]
176
+ @redis = options[:redis] || Redis.current
156
177
  end
157
178
 
158
179
  # Ruby defines a now deprecated type method so we need to override it here
@@ -163,11 +184,11 @@ class Redis
163
184
 
164
185
  alias_method :self_respond_to?, :respond_to?
165
186
 
166
- def respond_to?(*args)
167
- if self_respond_to?(*args)
187
+ def respond_to?(command, include_private=false)
188
+ if self_respond_to?(command, include_private)
168
189
  true
169
190
  else
170
- @redis.respond_to?(*args)
191
+ @redis.respond_to?(command, include_private)
171
192
  end
172
193
  end
173
194
 
@@ -175,10 +196,6 @@ class Redis
175
196
  query.nil? ? super("*") : super
176
197
  end
177
198
 
178
- def exec
179
- method_missing(:exec)
180
- end
181
-
182
199
  def method_missing(command, *args, &block)
183
200
  handling = COMMANDS[command.to_s] ||
184
201
  COMMANDS[ALIASES[command.to_s]]
@@ -205,6 +222,14 @@ class Redis
205
222
  last = args.pop
206
223
  args = add_namespace(args)
207
224
  args.push(last) if last
225
+ when :exclude_options
226
+ if args.last.is_a?(Hash)
227
+ last = args.pop
228
+ args = add_namespace(args)
229
+ args.push(last)
230
+ else
231
+ args = add_namespace(args)
232
+ end
208
233
  when :alternate
209
234
  args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
210
235
  when :sort
@@ -218,7 +243,12 @@ class Redis
218
243
  result = @redis.send(command, *args, &block)
219
244
 
220
245
  # Remove the namespace from results that are keys.
221
- result = rem_namespace(result) if after == :all
246
+ case after
247
+ when :all
248
+ result = rem_namespace(result)
249
+ when :first
250
+ result[0] = rem_namespace(result[0]) if result
251
+ end
222
252
 
223
253
  result
224
254
  end
data/spec/redis_spec.rb CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe "redis" do
4
4
  before(:all) do
5
- # use database 15 for testing so we dont accidentally step on you real data
5
+ # use database 15 for testing so we dont accidentally step on your real data
6
6
  @redis = Redis.new :db => 15
7
7
  end
8
8
 
@@ -37,6 +37,14 @@ describe "redis" do
37
37
  @namespaced.type('counter').should == 'string'
38
38
  end
39
39
 
40
+ it "should be able to use a namespace with bpop" do
41
+ @namespaced.rpush "foo", "string"
42
+ @namespaced.rpush "foo", "ns:string"
43
+ @namespaced.blpop("foo", 1).should == ["foo", "string"]
44
+ @namespaced.blpop("foo", 1).should == ["foo", "ns:string"]
45
+ @namespaced.blpop("foo", 1).should == nil
46
+ end
47
+
40
48
  it "should be able to use a namespace with del" do
41
49
  @namespaced['foo'] = 1000
42
50
  @namespaced['bar'] = 2000
@@ -48,6 +56,66 @@ describe "redis" do
48
56
  @namespaced['baz'].should == nil
49
57
  end
50
58
 
59
+ it 'should be able to use a namespace with append' do
60
+ @namespaced['foo'] = 'bar'
61
+ @namespaced.append('foo','n').should == 4
62
+ @namespaced['foo'].should == 'barn'
63
+ @redis['foo'].should == 'bar'
64
+ end
65
+
66
+ it 'should be able to use a namespace with brpoplpush' do
67
+ @namespaced.lpush('foo','bar')
68
+ @namespaced.brpoplpush('foo','bar',0).should == 'bar'
69
+ @namespaced.lrange('foo',0,-1).should == []
70
+ @namespaced.lrange('bar',0,-1).should == ['bar']
71
+ end
72
+
73
+ it 'should be able to use a namespace with getbit' do
74
+ @namespaced.set('foo','bar')
75
+ @namespaced.getbit('foo',1).should == 1
76
+ end
77
+
78
+ it 'should be able to use a namespace with getrange' do
79
+ @namespaced.set('foo','bar')
80
+ @namespaced.getrange('foo',0,-1).should == 'bar'
81
+ end
82
+
83
+ it 'should be able to use a namespace with linsert' do
84
+ @namespaced.rpush('foo','bar')
85
+ @namespaced.rpush('foo','barn')
86
+ @namespaced.rpush('foo','bart')
87
+ @namespaced.linsert('foo','BEFORE','barn','barf').should == 4
88
+ @namespaced.lrange('foo',0,-1).should == ['bar','barf','barn','bart']
89
+ end
90
+
91
+ it 'should be able to use a namespace with lpushx' do
92
+ @namespaced.lpushx('foo','bar').should == 0
93
+ @namespaced.lpush('foo','boo')
94
+ @namespaced.lpushx('foo','bar').should == 2
95
+ @namespaced.lrange('foo',0,-1).should == ['bar','boo']
96
+ end
97
+
98
+ it 'should be able to use a namespace with rpushx' do
99
+ @namespaced.rpushx('foo','bar').should == 0
100
+ @namespaced.lpush('foo','boo')
101
+ @namespaced.rpushx('foo','bar').should == 2
102
+ @namespaced.lrange('foo',0,-1).should == ['boo','bar']
103
+ end
104
+
105
+ it 'should be able to use a namespace with setbit' do
106
+ @namespaced.setbit('virgin_key', 1, 1)
107
+ @namespaced.exists('virgin_key').should be_true
108
+ @namespaced.get('virgin_key').should == @namespaced.getrange('virgin_key',0,-1)
109
+ end
110
+
111
+ it 'should be able to use a namespace with setrange' do
112
+ @namespaced.setrange('foo', 0, 'bar')
113
+ @namespaced['foo'].should == 'bar'
114
+
115
+ @namespaced.setrange('bar', 2, 'foo')
116
+ @namespaced['bar'].should == "\000\000foo"
117
+ end
118
+
51
119
  it "should be able to use a namespace with mget" do
52
120
  @namespaced['foo'] = 1000
53
121
  @namespaced['bar'] = 2000
@@ -88,6 +156,7 @@ describe "redis" do
88
156
  @namespaced.hkeys('foonx').should == %w{ nx }
89
157
  @namespaced.hvals('foonx').should == %w{ 10 }
90
158
  @namespaced.mapped_hmset('baz', {'key' => 'value', 'key1' => 'value1', 'a_number' => 4})
159
+ @namespaced.mapped_hmget('baz', 'key', 'key1', 'a_number').should == {'key' => 'value', 'key1' => 'value1', 'a_number' => '4'}
91
160
  @namespaced.hgetall('baz').should == {'key' => 'value', 'key1' => 'value1', 'a_number' => '4'}
92
161
  end
93
162
 
@@ -111,6 +180,26 @@ describe "redis" do
111
180
  @namespaced.sunion('foo', 'bar').sort.should == %w( 1 2 3 4 )
112
181
  end
113
182
 
183
+ it "should properly union two sorted sets with options" do
184
+ @namespaced.zadd('sort1', 1, 1)
185
+ @namespaced.zadd('sort1', 2, 2)
186
+ @namespaced.zadd('sort2', 2, 2)
187
+ @namespaced.zadd('sort2', 3, 3)
188
+ @namespaced.zadd('sort2', 4, 4)
189
+ @namespaced.zunionstore('union', ['sort1', 'sort2'], :weights => [2, 1])
190
+ @namespaced.zrevrange('union', 0, -1).should == %w( 2 4 3 1 )
191
+ end
192
+
193
+ it "should properly union two sorted sets without options" do
194
+ @namespaced.zadd('sort1', 1, 1)
195
+ @namespaced.zadd('sort1', 2, 2)
196
+ @namespaced.zadd('sort2', 2, 2)
197
+ @namespaced.zadd('sort2', 3, 3)
198
+ @namespaced.zadd('sort2', 4, 4)
199
+ @namespaced.zunionstore('union', ['sort1', 'sort2'])
200
+ @namespaced.zrevrange('union', 0, -1).should == %w( 4 2 3 1 )
201
+ end
202
+
114
203
  it "should add namespace to sort" do
115
204
  @namespaced.sadd('foo', 1)
116
205
  @namespaced.sadd('foo', 2)
metadata CHANGED
@@ -1,66 +1,119 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: redis-namespace
3
- version: !ruby/object:Gem::Version
4
- version: 1.1.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 0
10
+ version: 1.2.0
5
11
  platform: ruby
6
- authors:
12
+ authors:
7
13
  - Chris Wanstrath
14
+ - Terence Lee
8
15
  autorequire:
9
16
  bindir: bin
10
17
  cert_chain: []
11
- date: 2013-08-03 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
18
+
19
+ date: 2012-05-25 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
14
22
  name: redis
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - <
18
- - !ruby/object:Gem::Version
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
19
34
  version: 3.0.0
20
35
  type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
21
39
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - <
25
- - !ruby/object:Gem::Version
26
- version: 3.0.0
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
27
65
  description: |
28
66
  Adds a Redis::Namespace class which can be used to namespace calls
29
67
  to Redis. This is useful when using a single instance of Redis with
30
68
  multiple, different applications.
69
+
31
70
  email: chris@ozmm.org
32
71
  executables: []
72
+
33
73
  extensions: []
74
+
34
75
  extra_rdoc_files: []
35
- files:
76
+
77
+ files:
36
78
  - README.md
37
79
  - Rakefile
38
80
  - LICENSE
39
- - lib/redis/namespace.rb
40
81
  - lib/redis-namespace.rb
82
+ - lib/redis/namespace.rb
41
83
  - spec/redis_spec.rb
42
84
  - spec/spec_helper.rb
43
85
  homepage: http://github.com/defunkt/redis-namespace
44
86
  licenses: []
45
- metadata: {}
87
+
46
88
  post_install_message:
47
89
  rdoc_options: []
48
- require_paths:
90
+
91
+ require_paths:
49
92
  - lib
50
- required_ruby_version: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - '>='
58
- - !ruby/object:Gem::Version
59
- version: '0'
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
60
111
  requirements: []
112
+
61
113
  rubyforge_project:
62
- rubygems_version: 2.0.2
114
+ rubygems_version: 1.8.21
63
115
  signing_key:
64
- specification_version: 4
116
+ specification_version: 3
65
117
  summary: Namespaces Redis commands.
66
118
  test_files: []
119
+
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 4e21c69c01d7d32407ce889f986494a8d8bf5d5d
4
- data.tar.gz: 29e559ef8ee02ebf2ef626df363b7ce20856d405
5
- SHA512:
6
- metadata.gz: dad2aa13dfce40202b9c4ddb0ac2a7e53596f4d5264ac10e3e6e722c4ce01dc15fb7d37cc27ba47f9c63f44ac4a776f65d2f9c6075db24cccaaf3de048808801
7
- data.tar.gz: afcb9eb9c96f1384791c841cfe8bcfcff9e0e2566067c9e295e01e36c22f0442e3a7b395b85f1ce4b71aa781cd72f4ab6ad78a0cc435a15e45c116e43bae6de8