redis 0.2.0 → 1.0.0
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.
- data/README.markdown +64 -26
- data/Rakefile +6 -16
- data/lib/redis.rb +9 -8
- data/lib/redis/client.rb +202 -134
- data/lib/redis/dist_redis.rb +2 -2
- data/lib/redis/hash_ring.rb +1 -1
- data/lib/redis/pipeline.rb +1 -2
- metadata +3 -5
- data/spec/redis_spec.rb +0 -752
- data/spec/spec_helper.rb +0 -4
data/lib/redis/dist_redis.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'redis/hash_ring'
|
2
2
|
|
3
|
-
|
3
|
+
class Redis
|
4
4
|
class DistRedis
|
5
5
|
attr_reader :ring
|
6
6
|
def initialize(opts={})
|
@@ -111,4 +111,4 @@ module RedisRb
|
|
111
111
|
end
|
112
112
|
|
113
113
|
# For backwards compatibility
|
114
|
-
DistRedis =
|
114
|
+
DistRedis = Redis::DistRedis
|
data/lib/redis/hash_ring.rb
CHANGED
data/lib/redis/pipeline.rb
CHANGED
metadata
CHANGED
@@ -3,10 +3,10 @@ name: redis
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
|
-
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ezra Zygmuntowicz
|
@@ -19,7 +19,7 @@ autorequire: redis
|
|
19
19
|
bindir: bin
|
20
20
|
cert_chain: []
|
21
21
|
|
22
|
-
date: 2010-
|
22
|
+
date: 2010-04-02 00:00:00 -07:00
|
23
23
|
default_executable:
|
24
24
|
dependencies:
|
25
25
|
- !ruby/object:Gem::Dependency
|
@@ -54,8 +54,6 @@ files:
|
|
54
54
|
- lib/redis/raketasks.rb
|
55
55
|
- lib/redis.rb
|
56
56
|
- tasks/redis.tasks.rb
|
57
|
-
- spec/redis_spec.rb
|
58
|
-
- spec/spec_helper.rb
|
59
57
|
has_rdoc: true
|
60
58
|
homepage: http://github.com/ezmobius/redis-rb
|
61
59
|
licenses: []
|
data/spec/redis_spec.rb
DELETED
@@ -1,752 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'redis/raketasks'
|
3
|
-
require 'logger'
|
4
|
-
|
5
|
-
class Foo
|
6
|
-
attr_accessor :bar
|
7
|
-
def initialize(bar)
|
8
|
-
@bar = bar
|
9
|
-
end
|
10
|
-
|
11
|
-
def ==(other)
|
12
|
-
@bar == other.bar
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "redis" do
|
17
|
-
before(:all) do
|
18
|
-
result = RedisRunner.start_detached
|
19
|
-
raise("Could not start redis-server, aborting") unless result
|
20
|
-
|
21
|
-
# yea, this sucks, but it seems like sometimes we try to connect too quickly w/o it
|
22
|
-
sleep 1
|
23
|
-
|
24
|
-
# use database 15 for testing so we dont accidentally step on you real data
|
25
|
-
@r = Redis.new :db => 15
|
26
|
-
end
|
27
|
-
|
28
|
-
before(:each) do
|
29
|
-
@r['foo'] = 'bar'
|
30
|
-
end
|
31
|
-
|
32
|
-
after(:each) do
|
33
|
-
@r.keys('*').each {|k| @r.del k}
|
34
|
-
end
|
35
|
-
|
36
|
-
after(:all) do
|
37
|
-
begin
|
38
|
-
@r.quit
|
39
|
-
ensure
|
40
|
-
RedisRunner.stop
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should be able connect without a timeout" do
|
45
|
-
lambda { Redis.new :timeout => 0 }.should_not raise_error
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should be able to provide a logger" do
|
49
|
-
log = StringIO.new
|
50
|
-
r = Redis.new :db => 15, :logger => Logger.new(log)
|
51
|
-
r.ping
|
52
|
-
log.string.should include("ping")
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should be able to PING" do
|
56
|
-
@r.ping.should == 'PONG'
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should be able to GET a key" do
|
60
|
-
@r['foo'].should == 'bar'
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should be able to SET a key" do
|
64
|
-
@r['foo'] = 'nik'
|
65
|
-
@r['foo'].should == 'nik'
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should properly handle trailing newline characters" do
|
69
|
-
@r['foo'] = "bar\n"
|
70
|
-
@r['foo'].should == "bar\n"
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should store and retrieve all possible characters at the beginning and the end of a string" do
|
74
|
-
(0..255).each do |char_idx|
|
75
|
-
string = "#{char_idx.chr}---#{char_idx.chr}"
|
76
|
-
@r['foo'] = string
|
77
|
-
@r['foo'].should == string
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should be able to SET a key with an expiry" do
|
82
|
-
@r.set('foo', 'bar', 1)
|
83
|
-
@r['foo'].should == 'bar'
|
84
|
-
sleep 2
|
85
|
-
@r['foo'].should == nil
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should be able to return a TTL for a key" do
|
89
|
-
@r.set('foo', 'bar', 1)
|
90
|
-
@r.ttl('foo').should == 1
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should be able to SETNX" do
|
94
|
-
@r['foo'] = 'nik'
|
95
|
-
@r['foo'].should == 'nik'
|
96
|
-
@r.setnx 'foo', 'bar'
|
97
|
-
@r['foo'].should == 'nik'
|
98
|
-
end
|
99
|
-
#
|
100
|
-
it "should be able to GETSET" do
|
101
|
-
@r.getset('foo', 'baz').should == 'bar'
|
102
|
-
@r['foo'].should == 'baz'
|
103
|
-
end
|
104
|
-
#
|
105
|
-
it "should be able to INCR a key" do
|
106
|
-
@r.del('counter')
|
107
|
-
@r.incr('counter').should == 1
|
108
|
-
@r.incr('counter').should == 2
|
109
|
-
@r.incr('counter').should == 3
|
110
|
-
end
|
111
|
-
#
|
112
|
-
it "should be able to INCRBY a key" do
|
113
|
-
@r.del('counter')
|
114
|
-
@r.incrby('counter', 1).should == 1
|
115
|
-
@r.incrby('counter', 2).should == 3
|
116
|
-
@r.incrby('counter', 3).should == 6
|
117
|
-
end
|
118
|
-
#
|
119
|
-
it "should be able to DECR a key" do
|
120
|
-
@r.del('counter')
|
121
|
-
@r.incr('counter').should == 1
|
122
|
-
@r.incr('counter').should == 2
|
123
|
-
@r.incr('counter').should == 3
|
124
|
-
@r.decr('counter').should == 2
|
125
|
-
@r.decr('counter', 2).should == 0
|
126
|
-
end
|
127
|
-
#
|
128
|
-
it "should be able to RANDKEY" do
|
129
|
-
@r.randkey.should_not be_nil
|
130
|
-
end
|
131
|
-
#
|
132
|
-
it "should be able to RENAME a key" do
|
133
|
-
@r.del 'foo'
|
134
|
-
@r.del'bar'
|
135
|
-
@r['foo'] = 'hi'
|
136
|
-
@r.rename 'foo', 'bar'
|
137
|
-
@r['bar'].should == 'hi'
|
138
|
-
end
|
139
|
-
#
|
140
|
-
it "should be able to RENAMENX a key" do
|
141
|
-
@r.del 'foo'
|
142
|
-
@r.del 'bar'
|
143
|
-
@r['foo'] = 'hi'
|
144
|
-
@r['bar'] = 'ohai'
|
145
|
-
@r.renamenx 'foo', 'bar'
|
146
|
-
@r['bar'].should == 'ohai'
|
147
|
-
end
|
148
|
-
#
|
149
|
-
it "should be able to get DBSIZE of the database" do
|
150
|
-
@r.delete 'foo'
|
151
|
-
dbsize_without_foo = @r.dbsize
|
152
|
-
@r['foo'] = 0
|
153
|
-
dbsize_with_foo = @r.dbsize
|
154
|
-
|
155
|
-
dbsize_with_foo.should == dbsize_without_foo + 1
|
156
|
-
end
|
157
|
-
#
|
158
|
-
it "should be able to EXPIRE a key" do
|
159
|
-
@r['foo'] = 'bar'
|
160
|
-
@r.expire 'foo', 1
|
161
|
-
@r['foo'].should == "bar"
|
162
|
-
sleep 2
|
163
|
-
@r['foo'].should == nil
|
164
|
-
end
|
165
|
-
#
|
166
|
-
it "should be able to EXISTS" do
|
167
|
-
@r['foo'] = 'nik'
|
168
|
-
@r.exists('foo').should be_true
|
169
|
-
@r.del 'foo'
|
170
|
-
@r.exists('foo').should be_false
|
171
|
-
end
|
172
|
-
#
|
173
|
-
it "should be able to KEYS" do
|
174
|
-
@r.keys("f*").each { |key| @r.del key }
|
175
|
-
@r['f'] = 'nik'
|
176
|
-
@r['fo'] = 'nak'
|
177
|
-
@r['foo'] = 'qux'
|
178
|
-
@r.keys("f*").sort.should == ['f','fo', 'foo'].sort
|
179
|
-
end
|
180
|
-
#
|
181
|
-
it "should be able to return a random key (RANDOMKEY)" do
|
182
|
-
3.times { @r.exists(@r.randomkey).should be_true }
|
183
|
-
end
|
184
|
-
#
|
185
|
-
it "should be able to check the TYPE of a key" do
|
186
|
-
@r['foo'] = 'nik'
|
187
|
-
@r.type('foo').should == "string"
|
188
|
-
@r.del 'foo'
|
189
|
-
@r.type('foo').should == "none"
|
190
|
-
end
|
191
|
-
#
|
192
|
-
it "should be able to push to the head of a list (LPUSH)" do
|
193
|
-
@r.lpush "list", 'hello'
|
194
|
-
@r.lpush "list", 42
|
195
|
-
@r.type('list').should == "list"
|
196
|
-
@r.llen('list').should == 2
|
197
|
-
@r.lpop('list').should == '42'
|
198
|
-
end
|
199
|
-
#
|
200
|
-
it "should be able to push to the tail of a list (RPUSH)" do
|
201
|
-
@r.rpush "list", 'hello'
|
202
|
-
@r.type('list').should == "list"
|
203
|
-
@r.llen('list').should == 1
|
204
|
-
end
|
205
|
-
#
|
206
|
-
it "should be able to pop the tail of a list (RPOP)" do
|
207
|
-
@r.rpush "list", 'hello'
|
208
|
-
@r.rpush"list", 'goodbye'
|
209
|
-
@r.type('list').should == "list"
|
210
|
-
@r.llen('list').should == 2
|
211
|
-
@r.rpop('list').should == 'goodbye'
|
212
|
-
end
|
213
|
-
#
|
214
|
-
it "should be able to pop the head of a list (LPOP)" do
|
215
|
-
@r.rpush "list", 'hello'
|
216
|
-
@r.rpush "list", 'goodbye'
|
217
|
-
@r.type('list').should == "list"
|
218
|
-
@r.llen('list').should == 2
|
219
|
-
@r.lpop('list').should == 'hello'
|
220
|
-
end
|
221
|
-
#
|
222
|
-
it "should be able to get the length of a list (LLEN)" do
|
223
|
-
@r.rpush "list", 'hello'
|
224
|
-
@r.rpush "list", 'goodbye'
|
225
|
-
@r.type('list').should == "list"
|
226
|
-
@r.llen('list').should == 2
|
227
|
-
end
|
228
|
-
#
|
229
|
-
it "should be able to get a range of values from a list (LRANGE)" do
|
230
|
-
@r.rpush "list", 'hello'
|
231
|
-
@r.rpush "list", 'goodbye'
|
232
|
-
@r.rpush "list", '1'
|
233
|
-
@r.rpush "list", '2'
|
234
|
-
@r.rpush "list", '3'
|
235
|
-
@r.type('list').should == "list"
|
236
|
-
@r.llen('list').should == 5
|
237
|
-
@r.lrange('list', 2, -1).should == ['1', '2', '3']
|
238
|
-
end
|
239
|
-
#
|
240
|
-
it "should be able to trim a list (LTRIM)" do
|
241
|
-
@r.rpush "list", 'hello'
|
242
|
-
@r.rpush "list", 'goodbye'
|
243
|
-
@r.rpush "list", '1'
|
244
|
-
@r.rpush "list", '2'
|
245
|
-
@r.rpush "list", '3'
|
246
|
-
@r.type('list').should == "list"
|
247
|
-
@r.llen('list').should == 5
|
248
|
-
@r.ltrim 'list', 0, 1
|
249
|
-
@r.llen('list').should == 2
|
250
|
-
@r.lrange('list', 0, -1).should == ['hello', 'goodbye']
|
251
|
-
end
|
252
|
-
#
|
253
|
-
it "should be able to get a value by indexing into a list (LINDEX)" do
|
254
|
-
@r.rpush "list", 'hello'
|
255
|
-
@r.rpush "list", 'goodbye'
|
256
|
-
@r.type('list').should == "list"
|
257
|
-
@r.llen('list').should == 2
|
258
|
-
@r.lindex('list', 1).should == 'goodbye'
|
259
|
-
end
|
260
|
-
#
|
261
|
-
it "should be able to set a value by indexing into a list (LSET)" do
|
262
|
-
@r.rpush "list", 'hello'
|
263
|
-
@r.rpush "list", 'hello'
|
264
|
-
@r.type('list').should == "list"
|
265
|
-
@r.llen('list').should == 2
|
266
|
-
@r.lset('list', 1, 'goodbye').should == 'OK'
|
267
|
-
@r.lindex('list', 1).should == 'goodbye'
|
268
|
-
end
|
269
|
-
#
|
270
|
-
it "should be able to remove values from a list (LREM)" do
|
271
|
-
@r.rpush "list", 'hello'
|
272
|
-
@r.rpush "list", 'goodbye'
|
273
|
-
@r.type('list').should == "list"
|
274
|
-
@r.llen('list').should == 2
|
275
|
-
@r.lrem('list', 1, 'hello').should == 1
|
276
|
-
@r.lrange('list', 0, -1).should == ['goodbye']
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should be able to pop values from a list and push them onto a temp list(RPOPLPUSH)" do
|
280
|
-
@r.rpush "list", 'one'
|
281
|
-
@r.rpush "list", 'two'
|
282
|
-
@r.rpush "list", 'three'
|
283
|
-
@r.type('list').should == "list"
|
284
|
-
@r.llen('list').should == 3
|
285
|
-
@r.lrange('list',0,-1).should == ['one', 'two', 'three']
|
286
|
-
@r.lrange('tmp',0,-1).should == []
|
287
|
-
@r.rpoplpush('list', 'tmp').should == 'three'
|
288
|
-
@r.lrange('tmp',0,-1).should == ['three']
|
289
|
-
@r.rpoplpush('list', 'tmp').should == 'two'
|
290
|
-
@r.lrange('tmp',0,-1).should == ['two', 'three']
|
291
|
-
@r.rpoplpush('list', 'tmp').should == 'one'
|
292
|
-
@r.lrange('tmp',0,-1).should == ['one','two','three']
|
293
|
-
end
|
294
|
-
#
|
295
|
-
it "should be able add members to a set (SADD)" do
|
296
|
-
@r.sadd "set", 'key1'
|
297
|
-
@r.sadd "set", 'key2'
|
298
|
-
@r.type('set').should == "set"
|
299
|
-
@r.scard('set').should == 2
|
300
|
-
@r.smembers('set').sort.should == ['key1', 'key2'].sort
|
301
|
-
end
|
302
|
-
#
|
303
|
-
it "should be able delete members to a set (SREM)" do
|
304
|
-
@r.sadd "set", 'key1'
|
305
|
-
@r.sadd "set", 'key2'
|
306
|
-
@r.type('set').should == "set"
|
307
|
-
@r.scard('set').should == 2
|
308
|
-
@r.smembers('set').sort.should == ['key1', 'key2'].sort
|
309
|
-
@r.srem('set', 'key1')
|
310
|
-
@r.scard('set').should == 1
|
311
|
-
@r.smembers('set').should == ['key2']
|
312
|
-
end
|
313
|
-
#
|
314
|
-
it "should be able to return and remove random key from set (SPOP)" do
|
315
|
-
@r.sadd "set_pop", "key1"
|
316
|
-
@r.sadd "set_pop", "key2"
|
317
|
-
@r.spop("set_pop").should_not be_nil
|
318
|
-
@r.scard("set_pop").should == 1
|
319
|
-
end
|
320
|
-
#
|
321
|
-
it "should be able to return random key without delete the key from a set (SRANDMEMBER)" do
|
322
|
-
@r.sadd "set_srandmember", "key1"
|
323
|
-
@r.sadd "set_srandmember", "key2"
|
324
|
-
@r.srandmember("set_srandmember").should_not be_nil
|
325
|
-
@r.scard("set_srandmember").should == 2
|
326
|
-
end
|
327
|
-
#
|
328
|
-
it "should be able count the members of a set (SCARD)" do
|
329
|
-
@r.sadd "set", 'key1'
|
330
|
-
@r.sadd "set", 'key2'
|
331
|
-
@r.type('set').should == "set"
|
332
|
-
@r.scard('set').should == 2
|
333
|
-
end
|
334
|
-
#
|
335
|
-
it "should be able test for set membership (SISMEMBER)" do
|
336
|
-
@r.sadd "set", 'key1'
|
337
|
-
@r.sadd "set", 'key2'
|
338
|
-
@r.type('set').should == "set"
|
339
|
-
@r.scard('set').should == 2
|
340
|
-
@r.sismember('set', 'key1').should be_true
|
341
|
-
@r.sismember('set', 'key2').should be_true
|
342
|
-
@r.sismember('set', 'notthere').should be_false
|
343
|
-
end
|
344
|
-
#
|
345
|
-
it "should be able to do set intersection (SINTER)" do
|
346
|
-
@r.sadd "set", 'key1'
|
347
|
-
@r.sadd "set", 'key2'
|
348
|
-
@r.sadd "set2", 'key2'
|
349
|
-
@r.sinter('set', 'set2').should == ['key2']
|
350
|
-
end
|
351
|
-
#
|
352
|
-
it "should be able to do set intersection and store the results in a key (SINTERSTORE)" do
|
353
|
-
@r.sadd "set", 'key1'
|
354
|
-
@r.sadd "set", 'key2'
|
355
|
-
@r.sadd "set2", 'key2'
|
356
|
-
@r.sinterstore('newone', 'set', 'set2').should == 1
|
357
|
-
@r.smembers('newone').should == ['key2']
|
358
|
-
end
|
359
|
-
#
|
360
|
-
it "should be able to do set union (SUNION)" do
|
361
|
-
@r.sadd "set", 'key1'
|
362
|
-
@r.sadd "set", 'key2'
|
363
|
-
@r.sadd "set2", 'key2'
|
364
|
-
@r.sadd "set2", 'key3'
|
365
|
-
@r.sunion('set', 'set2').sort.should == ['key1','key2','key3'].sort
|
366
|
-
end
|
367
|
-
#
|
368
|
-
it "should be able to do set union and store the results in a key (SUNIONSTORE)" do
|
369
|
-
@r.sadd "set", 'key1'
|
370
|
-
@r.sadd "set", 'key2'
|
371
|
-
@r.sadd "set2", 'key2'
|
372
|
-
@r.sadd "set2", 'key3'
|
373
|
-
@r.sunionstore('newone', 'set', 'set2').should == 3
|
374
|
-
@r.smembers('newone').sort.should == ['key1','key2','key3'].sort
|
375
|
-
end
|
376
|
-
#
|
377
|
-
it "should be able to do set difference (SDIFF)" do
|
378
|
-
@r.sadd "set", 'a'
|
379
|
-
@r.sadd "set", 'b'
|
380
|
-
@r.sadd "set2", 'b'
|
381
|
-
@r.sadd "set2", 'c'
|
382
|
-
@r.sdiff('set', 'set2').should == ['a']
|
383
|
-
end
|
384
|
-
#
|
385
|
-
it "should be able to do set difference and store the results in a key (SDIFFSTORE)" do
|
386
|
-
@r.sadd "set", 'a'
|
387
|
-
@r.sadd "set", 'b'
|
388
|
-
@r.sadd "set2", 'b'
|
389
|
-
@r.sadd "set2", 'c'
|
390
|
-
@r.sdiffstore('newone', 'set', 'set2')
|
391
|
-
@r.smembers('newone').should == ['a']
|
392
|
-
end
|
393
|
-
#
|
394
|
-
it "should be able move elements from one set to another (SMOVE)" do
|
395
|
-
@r.sadd 'set1', 'a'
|
396
|
-
@r.sadd 'set1', 'b'
|
397
|
-
@r.sadd 'set2', 'x'
|
398
|
-
@r.smove('set1', 'set2', 'a').should be_true
|
399
|
-
@r.sismember('set2', 'a').should be_true
|
400
|
-
@r.delete('set1')
|
401
|
-
end
|
402
|
-
#
|
403
|
-
it "should be able to do crazy SORT queries" do
|
404
|
-
# The 'Dogs' is capitialized on purpose
|
405
|
-
@r['dog_1'] = 'louie'
|
406
|
-
@r.rpush 'Dogs', 1
|
407
|
-
@r['dog_2'] = 'lucy'
|
408
|
-
@r.rpush 'Dogs', 2
|
409
|
-
@r['dog_3'] = 'max'
|
410
|
-
@r.rpush 'Dogs', 3
|
411
|
-
@r['dog_4'] = 'taj'
|
412
|
-
@r.rpush 'Dogs', 4
|
413
|
-
@r.sort('Dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie']
|
414
|
-
@r.sort('Dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj']
|
415
|
-
end
|
416
|
-
|
417
|
-
it "should be able to handle array of :get using SORT" do
|
418
|
-
@r['dog:1:name'] = 'louie'
|
419
|
-
@r['dog:1:breed'] = 'mutt'
|
420
|
-
@r.rpush 'dogs', 1
|
421
|
-
@r['dog:2:name'] = 'lucy'
|
422
|
-
@r['dog:2:breed'] = 'poodle'
|
423
|
-
@r.rpush 'dogs', 2
|
424
|
-
@r['dog:3:name'] = 'max'
|
425
|
-
@r['dog:3:breed'] = 'hound'
|
426
|
-
@r.rpush 'dogs', 3
|
427
|
-
@r['dog:4:name'] = 'taj'
|
428
|
-
@r['dog:4:breed'] = 'terrier'
|
429
|
-
@r.rpush 'dogs', 4
|
430
|
-
@r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1]).should == ['louie', 'mutt']
|
431
|
-
@r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1], :order => 'desc alpha').should == ['taj', 'terrier']
|
432
|
-
end
|
433
|
-
|
434
|
-
it "should be able to store a SORT result" do
|
435
|
-
@r.rpush 'colors', 'red'
|
436
|
-
@r.rpush 'colors', 'yellow'
|
437
|
-
@r.rpush 'colors', 'orange'
|
438
|
-
@r.rpush 'colors', 'green'
|
439
|
-
@r.sort('colors', :limit => [1,2], :order => 'desc alpha', :store => 'sorted.colors')
|
440
|
-
@r.lrange('sorted.colors', 0, -1).should == ['red', 'orange']
|
441
|
-
end
|
442
|
-
#
|
443
|
-
it "should be able count the members of a zset" do
|
444
|
-
@r.set_add "set", 'key1'
|
445
|
-
@r.set_add "set", 'key2'
|
446
|
-
@r.zset_add 'zset', 1, 'set'
|
447
|
-
@r.zset_count('zset').should == 1
|
448
|
-
@r.delete('set')
|
449
|
-
@r.delete('zset')
|
450
|
-
end
|
451
|
-
#
|
452
|
-
it "should be able add members to a zset" do
|
453
|
-
@r.set_add "set", 'key1'
|
454
|
-
@r.set_add "set", 'key2'
|
455
|
-
@r.zset_add 'zset', 1, 'set'
|
456
|
-
@r.zset_range('zset', 0, 1).should == ['set']
|
457
|
-
@r.zset_count('zset').should == 1
|
458
|
-
@r.delete('set')
|
459
|
-
@r.delete('zset')
|
460
|
-
end
|
461
|
-
#
|
462
|
-
it "should be able delete members to a zset" do
|
463
|
-
@r.set_add "set", 'key1'
|
464
|
-
@r.set_add "set", 'key2'
|
465
|
-
@r.type?('set').should == "set"
|
466
|
-
@r.set_add "set2", 'key3'
|
467
|
-
@r.set_add "set2", 'key4'
|
468
|
-
@r.type?('set2').should == "set"
|
469
|
-
@r.zset_add 'zset', 1, 'set'
|
470
|
-
@r.zset_count('zset').should == 1
|
471
|
-
@r.zset_add 'zset', 2, 'set2'
|
472
|
-
@r.zset_count('zset').should == 2
|
473
|
-
@r.zset_delete 'zset', 'set'
|
474
|
-
@r.zset_count('zset').should == 1
|
475
|
-
@r.delete('set')
|
476
|
-
@r.delete('set2')
|
477
|
-
@r.delete('zset')
|
478
|
-
end
|
479
|
-
#
|
480
|
-
it "should be able to get a range of values from a zset" do
|
481
|
-
@r.set_add "set", 'key1'
|
482
|
-
@r.set_add "set", 'key2'
|
483
|
-
@r.set_add "set2", 'key3'
|
484
|
-
@r.set_add "set2", 'key4'
|
485
|
-
@r.set_add "set3", 'key1'
|
486
|
-
@r.type?('set').should == 'set'
|
487
|
-
@r.type?('set2').should == 'set'
|
488
|
-
@r.type?('set3').should == 'set'
|
489
|
-
@r.zset_add 'zset', 1, 'set'
|
490
|
-
@r.zset_add 'zset', 2, 'set2'
|
491
|
-
@r.zset_add 'zset', 3, 'set3'
|
492
|
-
@r.zset_count('zset').should == 3
|
493
|
-
@r.zset_range('zset', 0, 3).should == ['set', 'set2', 'set3']
|
494
|
-
@r.delete('set')
|
495
|
-
@r.delete('set2')
|
496
|
-
@r.delete('set3')
|
497
|
-
@r.delete('zset')
|
498
|
-
end
|
499
|
-
#
|
500
|
-
it "should be able to get a reverse range of values from a zset" do
|
501
|
-
@r.set_add "set", 'key1'
|
502
|
-
@r.set_add "set", 'key2'
|
503
|
-
@r.set_add "set2", 'key3'
|
504
|
-
@r.set_add "set2", 'key4'
|
505
|
-
@r.set_add "set3", 'key1'
|
506
|
-
@r.type?('set').should == 'set'
|
507
|
-
@r.type?('set2').should == 'set'
|
508
|
-
@r.type?('set3').should == 'set'
|
509
|
-
@r.zset_add 'zset', 1, 'set'
|
510
|
-
@r.zset_add 'zset', 2, 'set2'
|
511
|
-
@r.zset_add 'zset', 3, 'set3'
|
512
|
-
@r.zset_count('zset').should == 3
|
513
|
-
@r.zset_reverse_range('zset', 0, 3).should == ['set3', 'set2', 'set']
|
514
|
-
@r.delete('set')
|
515
|
-
@r.delete('set2')
|
516
|
-
@r.delete('set3')
|
517
|
-
@r.delete('zset')
|
518
|
-
end
|
519
|
-
#
|
520
|
-
it "should be able to get a range by score of values from a zset" do
|
521
|
-
@r.set_add "set", 'key1'
|
522
|
-
@r.set_add "set", 'key2'
|
523
|
-
@r.set_add "set2", 'key3'
|
524
|
-
@r.set_add "set2", 'key4'
|
525
|
-
@r.set_add "set3", 'key1'
|
526
|
-
@r.set_add "set4", 'key4'
|
527
|
-
@r.zset_add 'zset', 1, 'set'
|
528
|
-
@r.zset_add 'zset', 2, 'set2'
|
529
|
-
@r.zset_add 'zset', 3, 'set3'
|
530
|
-
@r.zset_add 'zset', 4, 'set4'
|
531
|
-
@r.zset_count('zset').should == 4
|
532
|
-
@r.zset_range_by_score('zset', 2, 3).should == ['set2', 'set3']
|
533
|
-
@r.delete('set')
|
534
|
-
@r.delete('set2')
|
535
|
-
@r.delete('set3')
|
536
|
-
@r.delete('set4')
|
537
|
-
@r.delete('zset')
|
538
|
-
end
|
539
|
-
#
|
540
|
-
it "should be able to get a score for a specific value in a zset (ZSCORE)" do
|
541
|
-
@r.zset_add "zset", 23, "value"
|
542
|
-
@r.zset_score("zset", "value").should == "23"
|
543
|
-
|
544
|
-
@r.zset_score("zset", "value2").should be_nil
|
545
|
-
@r.zset_score("unknown_zset", "value").should be_nil
|
546
|
-
|
547
|
-
@r.delete("zset")
|
548
|
-
end
|
549
|
-
#
|
550
|
-
it "should be able to increment a range score of a zset (ZINCRBY)" do
|
551
|
-
# create a new zset
|
552
|
-
@r.zset_increment_by "hackers", 1965, "Yukihiro Matsumoto"
|
553
|
-
@r.zset_score("hackers", "Yukihiro Matsumoto").should == "1965"
|
554
|
-
|
555
|
-
# add a new element
|
556
|
-
@r.zset_increment_by "hackers", 1912, "Alan Turing"
|
557
|
-
@r.zset_score("hackers", "Alan Turing").should == "1912"
|
558
|
-
|
559
|
-
# update the score
|
560
|
-
@r.zset_increment_by "hackers", 100, "Alan Turing" # yeah, we are making Turing a bit younger
|
561
|
-
@r.zset_score("hackers", "Alan Turing").should == "2012"
|
562
|
-
|
563
|
-
# attempt to update a key that's not a zset
|
564
|
-
@r["i_am_not_a_zet"] = "value"
|
565
|
-
lambda { @r.zset_incr_by "i_am_not_a_zet", 23, "element" }.should raise_error
|
566
|
-
|
567
|
-
@r.delete("hackers")
|
568
|
-
@r.delete("i_am_not_a_zet")
|
569
|
-
end
|
570
|
-
#
|
571
|
-
it "should provide info (INFO)" do
|
572
|
-
[:last_save_time, :redis_version, :total_connections_received, :connected_clients, :total_commands_processed, :connected_slaves, :uptime_in_seconds, :used_memory, :uptime_in_days, :changes_since_last_save].each do |x|
|
573
|
-
@r.info.keys.should include(x)
|
574
|
-
end
|
575
|
-
end
|
576
|
-
#
|
577
|
-
it "should be able to flush the database (FLUSHDB)" do
|
578
|
-
@r['key1'] = 'keyone'
|
579
|
-
@r['key2'] = 'keytwo'
|
580
|
-
@r.keys('*').sort.should == ['foo', 'key1', 'key2'].sort #foo from before
|
581
|
-
@r.flushdb
|
582
|
-
@r.keys('*').should == []
|
583
|
-
end
|
584
|
-
#
|
585
|
-
it "should raise exception when manually try to change the database" do
|
586
|
-
lambda { @r.select(0) }.should raise_error
|
587
|
-
end
|
588
|
-
#
|
589
|
-
it "should be able to provide the last save time (LASTSAVE)" do
|
590
|
-
savetime = @r.lastsave
|
591
|
-
Time.at(savetime).class.should == Time
|
592
|
-
Time.at(savetime).should <= Time.now
|
593
|
-
end
|
594
|
-
|
595
|
-
it "should be able to MGET keys" do
|
596
|
-
@r['foo'] = 1000
|
597
|
-
@r['bar'] = 2000
|
598
|
-
@r.mget('foo', 'bar').should == ['1000', '2000']
|
599
|
-
@r.mget('foo', 'bar', 'baz').should == ['1000', '2000', nil]
|
600
|
-
end
|
601
|
-
|
602
|
-
it "should be able to mapped MGET keys" do
|
603
|
-
@r['foo'] = 1000
|
604
|
-
@r['bar'] = 2000
|
605
|
-
@r.mapped_mget('foo', 'bar').should == { 'foo' => '1000', 'bar' => '2000'}
|
606
|
-
@r.mapped_mget('foo', 'baz', 'bar').should == { 'foo' => '1000', 'bar' => '2000'}
|
607
|
-
end
|
608
|
-
|
609
|
-
it "should be able to MSET values" do
|
610
|
-
@r.mset :key1 => "value1", :key2 => "value2"
|
611
|
-
@r['key1'].should == "value1"
|
612
|
-
@r['key2'].should == "value2"
|
613
|
-
end
|
614
|
-
|
615
|
-
it "should be able to MSETNX values" do
|
616
|
-
@r.msetnx :keynx1 => "valuenx1", :keynx2 => "valuenx2"
|
617
|
-
@r.mget('keynx1', 'keynx2').should == ["valuenx1", "valuenx2"]
|
618
|
-
|
619
|
-
@r["keynx1"] = "value1"
|
620
|
-
@r["keynx2"] = "value2"
|
621
|
-
@r.msetnx :keynx1 => "valuenx1", :keynx2 => "valuenx2"
|
622
|
-
@r.mget('keynx1', 'keynx2').should == ["value1", "value2"]
|
623
|
-
end
|
624
|
-
|
625
|
-
it "should bgsave" do
|
626
|
-
['OK', 'Background saving started'].include?( @r.bgsave).should == true
|
627
|
-
end
|
628
|
-
|
629
|
-
it "should be able to ECHO" do
|
630
|
-
@r.echo("message in a bottle\n").should == "message in a bottle\n"
|
631
|
-
end
|
632
|
-
|
633
|
-
it "should raise error when invoke MONITOR" do
|
634
|
-
lambda { @r.monitor }.should raise_error
|
635
|
-
end
|
636
|
-
|
637
|
-
it "should raise error when invoke SYNC" do
|
638
|
-
lambda { @r.sync }.should raise_error
|
639
|
-
end
|
640
|
-
|
641
|
-
it "should handle multiple servers" do
|
642
|
-
require 'redis/dist_redis'
|
643
|
-
@r = DistRedis.new(:hosts=> ['localhost:6379', '127.0.0.1:6379'], :db => 15)
|
644
|
-
|
645
|
-
100.times do |idx|
|
646
|
-
@r[idx] = "foo#{idx}"
|
647
|
-
end
|
648
|
-
|
649
|
-
100.times do |idx|
|
650
|
-
@r[idx].should == "foo#{idx}"
|
651
|
-
end
|
652
|
-
|
653
|
-
@r.keys('*').sort.first.should == "0"
|
654
|
-
end
|
655
|
-
|
656
|
-
it "should be able to pipeline writes" do
|
657
|
-
@r.pipelined do |pipeline|
|
658
|
-
pipeline.lpush 'list', "hello"
|
659
|
-
pipeline.lpush 'list', 42
|
660
|
-
end
|
661
|
-
|
662
|
-
@r.type('list').should == "list"
|
663
|
-
@r.llen('list').should == 2
|
664
|
-
@r.lpop('list').should == '42'
|
665
|
-
end
|
666
|
-
|
667
|
-
it "should do nothing when pipelining no commands" do
|
668
|
-
@r.pipelined do |pipeline|
|
669
|
-
end
|
670
|
-
end
|
671
|
-
|
672
|
-
it "should AUTH when connecting with a password" do
|
673
|
-
r = Redis.new(:password => 'secret')
|
674
|
-
r.stub!(:connect_to)
|
675
|
-
r.should_receive(:call_command).with(['auth', 'secret'])
|
676
|
-
r.connect_to_server
|
677
|
-
end
|
678
|
-
|
679
|
-
it "should run MULTI without a block" do
|
680
|
-
@r.multi
|
681
|
-
@r.get("key1").should == "QUEUED"
|
682
|
-
@r.discard
|
683
|
-
end
|
684
|
-
|
685
|
-
it "should run MULTI/EXEC with a block" do
|
686
|
-
@r.multi do
|
687
|
-
@r.set "key1", "value1"
|
688
|
-
end
|
689
|
-
|
690
|
-
@r.get("key1").should == "value1"
|
691
|
-
|
692
|
-
begin
|
693
|
-
@r.multi do
|
694
|
-
@r.set "key2", "value2"
|
695
|
-
raise "Some error"
|
696
|
-
@r.set "key3", "value3"
|
697
|
-
end
|
698
|
-
rescue
|
699
|
-
end
|
700
|
-
|
701
|
-
@r.get("key2").should be_nil
|
702
|
-
@r.get("key3").should be_nil
|
703
|
-
end
|
704
|
-
|
705
|
-
it "should yield the Redis object when using #multi with a block" do
|
706
|
-
@r.multi do |multi|
|
707
|
-
multi.set "key1", "value1"
|
708
|
-
end
|
709
|
-
|
710
|
-
@r.get("key1").should == "value1"
|
711
|
-
end
|
712
|
-
|
713
|
-
it "can set and get hash values" do
|
714
|
-
@r.hset("rush", "signals", "1982").should be_true
|
715
|
-
@r.hexists("rush", "signals").should be_true
|
716
|
-
@r.hget("rush", "signals").should == "1982"
|
717
|
-
end
|
718
|
-
|
719
|
-
it "can delete hash values" do
|
720
|
-
@r.hset("rush", "YYZ", "1981")
|
721
|
-
@r.hdel("rush", "YYZ").should be_true
|
722
|
-
@r.hexists("rush", "YYZ").should be_false
|
723
|
-
end
|
724
|
-
|
725
|
-
describe "with some hash values" do
|
726
|
-
before(:each) do
|
727
|
-
@r.hset("rush", "permanent waves", "1980")
|
728
|
-
@r.hset("rush", "moving pictures", "1981")
|
729
|
-
@r.hset("rush", "signals", "1982")
|
730
|
-
end
|
731
|
-
|
732
|
-
it "can get the length of the hash" do
|
733
|
-
@r.hlen("rush").should == 3
|
734
|
-
@r.hlen("yyz").should be_zero
|
735
|
-
end
|
736
|
-
|
737
|
-
it "can get the keys and values of the hash" do
|
738
|
-
@r.hkeys("rush").should == ["permanent waves", "moving pictures", "signals"]
|
739
|
-
@r.hvals("rush").should == %w[1980 1981 1982]
|
740
|
-
@r.hvals("yyz").should be_empty
|
741
|
-
end
|
742
|
-
|
743
|
-
it "returns a hash for HGETALL" do
|
744
|
-
@r.hgetall("rush").should == {
|
745
|
-
"permanent waves" => "1980",
|
746
|
-
"moving pictures" => "1981",
|
747
|
-
"signals" => "1982"
|
748
|
-
}
|
749
|
-
@r.hgetall("yyz").should == {}
|
750
|
-
end
|
751
|
-
end
|
752
|
-
end
|