redis 3.0.0.rc2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -110,26 +110,15 @@ class Redis
110
110
  end
111
111
 
112
112
  def call_pipeline(pipeline)
113
- without_reconnect_wrapper = lambda do |&blk| blk.call end
114
- without_reconnect_wrapper = lambda do |&blk|
115
- without_reconnect(&blk)
116
- end if pipeline.without_reconnect?
117
-
118
- shutdown_wrapper = lambda do |&blk| blk.call end
119
- shutdown_wrapper = lambda do |&blk|
113
+ with_reconnect pipeline.with_reconnect? do
120
114
  begin
121
- blk.call
122
- rescue ConnectionError
115
+ pipeline.finish(call_pipelined(pipeline.commands))
116
+ rescue ConnectionError => e
117
+ return nil if pipeline.shutdown?
123
118
  # Assume the pipeline was sent in one piece, but execution of
124
119
  # SHUTDOWN caused none of the replies for commands that were executed
125
120
  # prior to it from coming back around.
126
- nil
127
- end
128
- end if pipeline.shutdown?
129
-
130
- without_reconnect_wrapper.call do
131
- shutdown_wrapper.call do
132
- pipeline.finish(call_pipelined(pipeline.commands))
121
+ raise e
133
122
  end
134
123
  end
135
124
  end
@@ -234,15 +223,19 @@ class Redis
234
223
  end
235
224
  end
236
225
 
237
- def without_reconnect
226
+ def with_reconnect(val=true)
238
227
  begin
239
- original, @reconnect = @reconnect, false
228
+ original, @reconnect = @reconnect, val
240
229
  yield
241
230
  ensure
242
231
  @reconnect = original
243
232
  end
244
233
  end
245
234
 
235
+ def without_reconnect(&blk)
236
+ with_reconnect(false, &blk)
237
+ end
238
+
246
239
  protected
247
240
 
248
241
  def logging(commands)
@@ -34,11 +34,6 @@ class Redis
34
34
  @ring.add_node Redis.new(@default_options.merge(:url => url))
35
35
  end
36
36
 
37
- # Close the connection.
38
- def quit
39
- on_each_node :quit
40
- end
41
-
42
37
  # Change the selected database for the current connection.
43
38
  def select(db)
44
39
  on_each_node :select, db
@@ -49,14 +44,94 @@ class Redis
49
44
  on_each_node :ping
50
45
  end
51
46
 
47
+ # Echo the given string.
48
+ def echo(value)
49
+ on_each_node :echo, value
50
+ end
51
+
52
+ # Close the connection.
53
+ def quit
54
+ on_each_node :quit
55
+ end
56
+
57
+ # Asynchronously save the dataset to disk.
58
+ def bgsave
59
+ on_each_node :bgsave
60
+ end
61
+
62
+ # Return the number of keys in the selected database.
63
+ def dbsize
64
+ on_each_node :dbsize
65
+ end
66
+
52
67
  # Remove all keys from all databases.
53
68
  def flushall
54
69
  on_each_node :flushall
55
70
  end
56
71
 
57
- # Determine if a key exists.
58
- def exists(key)
59
- node_for(key).exists(key)
72
+ # Remove all keys from the current database.
73
+ def flushdb
74
+ on_each_node :flushdb
75
+ end
76
+
77
+ # Get information and statistics about the server.
78
+ def info(cmd = nil)
79
+ on_each_node :info, cmd
80
+ end
81
+
82
+ # Get the UNIX time stamp of the last successful save to disk.
83
+ def lastsave
84
+ on_each_node :lastsave
85
+ end
86
+
87
+ # Listen for all requests received by the server in real time.
88
+ def monitor
89
+ raise NotImplementedError
90
+ end
91
+
92
+ # Synchronously save the dataset to disk.
93
+ def save
94
+ on_each_node :save
95
+ end
96
+
97
+ # Get server time: an UNIX timestamp and the elapsed microseconds in the current second.
98
+ def time
99
+ on_each_node :time
100
+ end
101
+
102
+ # Remove the expiration from a key.
103
+ def persist(key)
104
+ node_for(key).persist(key)
105
+ end
106
+
107
+ # Set a key's time to live in seconds.
108
+ def expire(key, seconds)
109
+ node_for(key).expire(key, seconds)
110
+ end
111
+
112
+ # Set the expiration for a key as a UNIX timestamp.
113
+ def expireat(key, unix_time)
114
+ node_for(key).expireat(key, unix_time)
115
+ end
116
+
117
+ # Get the time to live (in seconds) for a key.
118
+ def ttl(key)
119
+ node_for(key).ttl(key)
120
+ end
121
+
122
+ # Set a key's time to live in milliseconds.
123
+ def pexpire(key, milliseconds)
124
+ node_for(key).pexpire(key, milliseconds)
125
+ end
126
+
127
+ # Set the expiration for a key as number of milliseconds from UNIX Epoch.
128
+ def pexpireat(key, ms_unix_time)
129
+ node_for(key).pexpireat(key, ms_unix_time)
130
+ end
131
+
132
+ # Get the time to live (in milliseconds) for a key.
133
+ def pttl(key)
134
+ node_for(key).pttl(key)
60
135
  end
61
136
 
62
137
  # Delete a key.
@@ -67,9 +142,9 @@ class Redis
67
142
  end
68
143
  end
69
144
 
70
- # Determine the type stored at key.
71
- def type(key)
72
- node_for(key).type(key)
145
+ # Determine if a key exists.
146
+ def exists(key)
147
+ node_for(key).exists(key)
73
148
  end
74
149
 
75
150
  # Find all keys matching the given pattern.
@@ -77,6 +152,11 @@ class Redis
77
152
  on_each_node(:keys, glob).flatten
78
153
  end
79
154
 
155
+ # Move a key to another database.
156
+ def move(key, db)
157
+ node_for(key).move(key, db)
158
+ end
159
+
80
160
  # Return a random key from the keyspace.
81
161
  def randomkey
82
162
  raise CannotDistribute, :randomkey
@@ -96,54 +176,43 @@ class Redis
96
176
  end
97
177
  end
98
178
 
99
- # Return the number of keys in the selected database.
100
- def dbsize
101
- on_each_node :dbsize
102
- end
103
-
104
- # Set a key's time to live in seconds.
105
- def expire(key, seconds)
106
- node_for(key).expire(key, seconds)
107
- end
108
-
109
- # Set a key's time to live in milliseconds.
110
- def pexpire(key, milliseconds)
111
- node_for(key).pexpire(key, milliseconds)
112
- end
179
+ # Sort the elements in a list, set or sorted set.
180
+ def sort(key, options = {})
181
+ keys = [key, options[:by], options[:store], *Array(options[:get])].compact
113
182
 
114
- # Set the expiration for a key as a UNIX timestamp.
115
- def expireat(key, unix_time)
116
- node_for(key).expireat(key, unix_time)
183
+ ensure_same_node(:sort, keys) do |node|
184
+ node.sort(key, options)
185
+ end
117
186
  end
118
187
 
119
- # Set the expiration for a key as number of milliseconds from UNIX Epoch.
120
- def pexpireat(key, ms_unix_time)
121
- node_for(key).pexpireat(key, ms_unix_time)
188
+ # Determine the type stored at key.
189
+ def type(key)
190
+ node_for(key).type(key)
122
191
  end
123
192
 
124
- # Remove the expiration from a key.
125
- def persist(key)
126
- node_for(key).persist(key)
193
+ # Decrement the integer value of a key by one.
194
+ def decr(key)
195
+ node_for(key).decr(key)
127
196
  end
128
197
 
129
- # Get the time to live (in seconds) for a key.
130
- def ttl(key)
131
- node_for(key).ttl(key)
198
+ # Decrement the integer value of a key by the given number.
199
+ def decrby(key, decrement)
200
+ node_for(key).decrby(key, decrement)
132
201
  end
133
202
 
134
- # Get the time to live (in milliseconds) for a key.
135
- def pttl(key)
136
- node_for(key).pttl(key)
203
+ # Increment the integer value of a key by one.
204
+ def incr(key)
205
+ node_for(key).incr(key)
137
206
  end
138
207
 
139
- # Move a key to another database.
140
- def move(key, db)
141
- node_for(key).move(key, db)
208
+ # Increment the integer value of a key by the given integer number.
209
+ def incrby(key, increment)
210
+ node_for(key).incrby(key, increment)
142
211
  end
143
212
 
144
- # Remove all keys from the current database.
145
- def flushdb
146
- on_each_node :flushdb
213
+ # Increment the numeric value of a key by the given float number.
214
+ def incrbyfloat(key, increment)
215
+ node_for(key).incrbyfloat(key, increment)
147
216
  end
148
217
 
149
218
  # Set the string value of a key.
@@ -151,16 +220,6 @@ class Redis
151
220
  node_for(key).set(key, value)
152
221
  end
153
222
 
154
- # Sets or clears the bit at offset in the string value stored at key.
155
- def setbit(key, offset, value)
156
- node_for(key).setbit(key, offset, value)
157
- end
158
-
159
- # Overwrite part of a string at key starting at the specified offset.
160
- def setrange(key, offset, value)
161
- node_for(key).setrange(key, offset, value)
162
- end
163
-
164
223
  # Set the time to live in seconds of a key.
165
224
  def setex(key, ttl, value)
166
225
  node_for(key).setex(key, ttl, value)
@@ -171,37 +230,32 @@ class Redis
171
230
  node_for(key).psetex(key, ttl, value)
172
231
  end
173
232
 
174
- # Get the value of a key.
175
- def get(key)
176
- node_for(key).get(key)
177
- end
178
-
179
- # Returns the bit value at offset in the string value stored at key.
180
- def getbit(key, offset)
181
- node_for(key).getbit(key, offset)
233
+ # Set the value of a key, only if the key does not exist.
234
+ def setnx(key, value)
235
+ node_for(key).setnx(key, value)
182
236
  end
183
237
 
184
- # Get a substring of the string stored at a key.
185
- def getrange(key, start, stop)
186
- node_for(key).getrange(key, start, stop)
238
+ # Set multiple keys to multiple values.
239
+ def mset(*args)
240
+ raise CannotDistribute, :mset
187
241
  end
188
242
 
189
- # Set the string value of a key and return its old value.
190
- def getset(key, value)
191
- node_for(key).getset(key, value)
243
+ def mapped_mset(hash)
244
+ raise CannotDistribute, :mapped_mset
192
245
  end
193
246
 
194
- def [](key)
195
- get(key)
247
+ # Set multiple keys to multiple values, only if none of the keys exist.
248
+ def msetnx(*args)
249
+ raise CannotDistribute, :msetnx
196
250
  end
197
251
 
198
- # Append a value to a key.
199
- def append(key, value)
200
- node_for(key).append(key, value)
252
+ def mapped_msetnx(hash)
253
+ raise CannotDistribute, :mapped_msetnx
201
254
  end
202
255
 
203
- def []=(key,value)
204
- set(key, value)
256
+ # Get the value of a key.
257
+ def get(key)
258
+ node_for(key).get(key)
205
259
  end
206
260
 
207
261
  # Get the values of all the given keys.
@@ -213,62 +267,47 @@ class Redis
213
267
  raise CannotDistribute, :mapped_mget
214
268
  end
215
269
 
216
- # Set the value of a key, only if the key does not exist.
217
- def setnx(key, value)
218
- node_for(key).setnx(key, value)
219
- end
220
-
221
- # Set multiple keys to multiple values.
222
- def mset(*args)
223
- raise CannotDistribute, :mset
224
- end
225
-
226
- def mapped_mset(hash)
227
- raise CannotDistribute, :mapped_mset
228
- end
229
-
230
- # Set multiple keys to multiple values, only if none of the keys exist.
231
- def msetnx(*args)
232
- raise CannotDistribute, :msetnx
270
+ # Overwrite part of a string at key starting at the specified offset.
271
+ def setrange(key, offset, value)
272
+ node_for(key).setrange(key, offset, value)
233
273
  end
234
274
 
235
- def mapped_msetnx(hash)
236
- raise CannotDistribute, :mapped_msetnx
275
+ # Get a substring of the string stored at a key.
276
+ def getrange(key, start, stop)
277
+ node_for(key).getrange(key, start, stop)
237
278
  end
238
279
 
239
- # Increment the integer value of a key by one.
240
- def incr(key)
241
- node_for(key).incr(key)
280
+ # Sets or clears the bit at offset in the string value stored at key.
281
+ def setbit(key, offset, value)
282
+ node_for(key).setbit(key, offset, value)
242
283
  end
243
284
 
244
- # Increment the integer value of a key by the given integer number.
245
- def incrby(key, increment)
246
- node_for(key).incrby(key, increment)
285
+ # Returns the bit value at offset in the string value stored at key.
286
+ def getbit(key, offset)
287
+ node_for(key).getbit(key, offset)
247
288
  end
248
289
 
249
- # Increment the numeric value of a key by the given float number.
250
- def incrbyfloat(key, increment)
251
- node_for(key).incrbyfloat(key, increment)
290
+ # Append a value to a key.
291
+ def append(key, value)
292
+ node_for(key).append(key, value)
252
293
  end
253
294
 
254
- # Decrement the integer value of a key by one.
255
- def decr(key)
256
- node_for(key).decr(key)
295
+ # Set the string value of a key and return its old value.
296
+ def getset(key, value)
297
+ node_for(key).getset(key, value)
257
298
  end
258
299
 
259
- # Decrement the integer value of a key by the given number.
260
- def decrby(key, decrement)
261
- node_for(key).decrby(key, decrement)
300
+ # Get the length of the value stored in a key.
301
+ def strlen(key)
302
+ node_for(key).strlen(key)
262
303
  end
263
304
 
264
- # Append one or more values to a list.
265
- def rpush(key, value)
266
- node_for(key).rpush(key, value)
305
+ def [](key)
306
+ get(key)
267
307
  end
268
308
 
269
- # Prepend one or more values to a list.
270
- def lpush(key, value)
271
- node_for(key).lpush(key, value)
309
+ def []=(key,value)
310
+ set(key, value)
272
311
  end
273
312
 
274
313
  # Get the length of a list.
@@ -276,29 +315,24 @@ class Redis
276
315
  node_for(key).llen(key)
277
316
  end
278
317
 
279
- # Get a range of elements from a list.
280
- def lrange(key, start, stop)
281
- node_for(key).lrange(key, start, stop)
282
- end
283
-
284
- # Trim a list to the specified range.
285
- def ltrim(key, start, stop)
286
- node_for(key).ltrim(key, start, stop)
318
+ # Prepend one or more values to a list.
319
+ def lpush(key, value)
320
+ node_for(key).lpush(key, value)
287
321
  end
288
322
 
289
- # Get an element from a list by its index.
290
- def lindex(key, index)
291
- node_for(key).lindex(key, index)
323
+ # Prepend a value to a list, only if the list exists.
324
+ def lpushx(key, value)
325
+ node_for(key).lpushx(key, value)
292
326
  end
293
327
 
294
- # Set the value of an element in a list by its index.
295
- def lset(key, index, value)
296
- node_for(key).lset(key, index, value)
328
+ # Append one or more values to a list.
329
+ def rpush(key, value)
330
+ node_for(key).rpush(key, value)
297
331
  end
298
332
 
299
- # Remove elements from a list.
300
- def lrem(key, count, value)
301
- node_for(key).lrem(key, count, value)
333
+ # Append a value to a list, only if the list exists.
334
+ def rpushx(key, value)
335
+ node_for(key).rpushx(key, value)
302
336
  end
303
337
 
304
338
  # Remove and get the first element in a list.
@@ -367,6 +401,41 @@ class Redis
367
401
  end
368
402
  end
369
403
 
404
+ # Get an element from a list by its index.
405
+ def lindex(key, index)
406
+ node_for(key).lindex(key, index)
407
+ end
408
+
409
+ # Insert an element before or after another element in a list.
410
+ def linsert(key, where, pivot, value)
411
+ node_for(key).linsert(key, where, pivot, value)
412
+ end
413
+
414
+ # Get a range of elements from a list.
415
+ def lrange(key, start, stop)
416
+ node_for(key).lrange(key, start, stop)
417
+ end
418
+
419
+ # Remove elements from a list.
420
+ def lrem(key, count, value)
421
+ node_for(key).lrem(key, count, value)
422
+ end
423
+
424
+ # Set the value of an element in a list by its index.
425
+ def lset(key, index, value)
426
+ node_for(key).lset(key, index, value)
427
+ end
428
+
429
+ # Trim a list to the specified range.
430
+ def ltrim(key, start, stop)
431
+ node_for(key).ltrim(key, start, stop)
432
+ end
433
+
434
+ # Get the number of members in a set.
435
+ def scard(key)
436
+ node_for(key).scard(key)
437
+ end
438
+
370
439
  # Add one or more members to a set.
371
440
  def sadd(key, member)
372
441
  node_for(key).sadd(key, member)
@@ -382,6 +451,11 @@ class Redis
382
451
  node_for(key).spop(key)
383
452
  end
384
453
 
454
+ # Get a random member from a set.
455
+ def srandmember(key)
456
+ node_for(key).srandmember(key)
457
+ end
458
+
385
459
  # Move a member from one set to another.
386
460
  def smove(source, destination, member)
387
461
  ensure_same_node(:smove, [source, destination]) do |node|
@@ -389,16 +463,30 @@ class Redis
389
463
  end
390
464
  end
391
465
 
392
- # Get the number of members in a set.
393
- def scard(key)
394
- node_for(key).scard(key)
395
- end
396
-
397
466
  # Determine if a given value is a member of a set.
398
467
  def sismember(key, member)
399
468
  node_for(key).sismember(key, member)
400
469
  end
401
470
 
471
+ # Get all the members in a set.
472
+ def smembers(key)
473
+ node_for(key).smembers(key)
474
+ end
475
+
476
+ # Subtract multiple sets.
477
+ def sdiff(*keys)
478
+ ensure_same_node(:sdiff, keys) do |node|
479
+ node.sdiff(*keys)
480
+ end
481
+ end
482
+
483
+ # Subtract multiple sets and store the resulting set in a key.
484
+ def sdiffstore(destination, *keys)
485
+ ensure_same_node(:sdiffstore, [destination] + keys) do |node|
486
+ node.sdiffstore(destination, *keys)
487
+ end
488
+ end
489
+
402
490
  # Intersect multiple sets.
403
491
  def sinter(*keys)
404
492
  ensure_same_node(:sinter, keys) do |node|
@@ -427,28 +515,9 @@ class Redis
427
515
  end
428
516
  end
429
517
 
430
- # Subtract multiple sets.
431
- def sdiff(*keys)
432
- ensure_same_node(:sdiff, keys) do |node|
433
- node.sdiff(*keys)
434
- end
435
- end
436
-
437
- # Subtract multiple sets and store the resulting set in a key.
438
- def sdiffstore(destination, *keys)
439
- ensure_same_node(:sdiffstore, [destination] + keys) do |node|
440
- node.sdiffstore(destination, *keys)
441
- end
442
- end
443
-
444
- # Get all the members in a set.
445
- def smembers(key)
446
- node_for(key).smembers(key)
447
- end
448
-
449
- # Get a random member from a set.
450
- def srandmember(key)
451
- node_for(key).srandmember(key)
518
+ # Get the number of members in a sorted set.
519
+ def zcard(key)
520
+ node_for(key).zcard(key)
452
521
  end
453
522
 
454
523
  # Add one or more members to a sorted set, or update the score for members
@@ -457,14 +526,19 @@ class Redis
457
526
  node_for(key).zadd(key, *args)
458
527
  end
459
528
 
529
+ # Increment the score of a member in a sorted set.
530
+ def zincrby(key, increment, member)
531
+ node_for(key).zincrby(key, increment, member)
532
+ end
533
+
460
534
  # Remove one or more members from a sorted set.
461
535
  def zrem(key, member)
462
536
  node_for(key).zrem(key, member)
463
537
  end
464
538
 
465
- # Increment the score of a member in a sorted set.
466
- def zincrby(key, increment, member)
467
- node_for(key).zincrby(key, increment, member)
539
+ # Get the score associated with the given member in a sorted set.
540
+ def zscore(key, member)
541
+ node_for(key).zscore(key, member)
468
542
  end
469
543
 
470
544
  # Return a range of members in a sorted set, by index.
@@ -472,6 +546,12 @@ class Redis
472
546
  node_for(key).zrange(key, start, stop, options)
473
547
  end
474
548
 
549
+ # Return a range of members in a sorted set, by index, with scores ordered
550
+ # from high to low.
551
+ def zrevrange(key, start, stop, options = {})
552
+ node_for(key).zrevrange(key, start, stop, options)
553
+ end
554
+
475
555
  # Determine the index of a member in a sorted set.
476
556
  def zrank(key, member)
477
557
  node_for(key).zrank(key, member)
@@ -483,17 +563,6 @@ class Redis
483
563
  node_for(key).zrevrank(key, member)
484
564
  end
485
565
 
486
- # Return a range of members in a sorted set, by index, with scores ordered
487
- # from high to low.
488
- def zrevrange(key, start, stop, options = {})
489
- node_for(key).zrevrange(key, start, stop, options)
490
- end
491
-
492
- # Remove all members in a sorted set within the given scores.
493
- def zremrangebyscore(key, min, max)
494
- node_for(key).zremrangebyscore(key, min, max)
495
- end
496
-
497
566
  # Remove all members in a sorted set within the given indexes.
498
567
  def zremrangebyrank(key, start, stop)
499
568
  node_for(key).zremrangebyrank(key, start, stop)
@@ -510,9 +579,9 @@ class Redis
510
579
  node_for(key).zrevrangebyscore(key, max, min, options)
511
580
  end
512
581
 
513
- # Get the number of members in a sorted set.
514
- def zcard(key)
515
- node_for(key).zcard(key)
582
+ # Remove all members in a sorted set within the given scores.
583
+ def zremrangebyscore(key, min, max)
584
+ node_for(key).zremrangebyscore(key, min, max)
516
585
  end
517
586
 
518
587
  # Get the number of members in a particular score range.
@@ -520,11 +589,6 @@ class Redis
520
589
  node_for(key).zcount(key, min, max)
521
590
  end
522
591
 
523
- # Get the score associated with the given member in a sorted set.
524
- def zscore(key, member)
525
- node_for(key).zscore(key, member)
526
- end
527
-
528
592
  # Intersect multiple sorted sets and store the resulting sorted set in a new
529
593
  # key.
530
594
  def zinterstore(destination, keys, options = {})
@@ -540,6 +604,11 @@ class Redis
540
604
  end
541
605
  end
542
606
 
607
+ # Get the number of fields in a hash.
608
+ def hlen(key)
609
+ node_for(key).hlen(key)
610
+ end
611
+
543
612
  # Set the string value of a hash field.
544
613
  def hset(key, field, value)
545
614
  node_for(key).hset(key, field, value)
@@ -550,41 +619,6 @@ class Redis
550
619
  node_for(key).hsetnx(key, field, value)
551
620
  end
552
621
 
553
- # Get the value of a hash field.
554
- def hget(key, field)
555
- node_for(key).hget(key, field)
556
- end
557
-
558
- # Delete one or more hash fields.
559
- def hdel(key, field)
560
- node_for(key).hdel(key, field)
561
- end
562
-
563
- # Determine if a hash field exists.
564
- def hexists(key, field)
565
- node_for(key).hexists(key, field)
566
- end
567
-
568
- # Get the number of fields in a hash.
569
- def hlen(key)
570
- node_for(key).hlen(key)
571
- end
572
-
573
- # Get all the fields in a hash.
574
- def hkeys(key)
575
- node_for(key).hkeys(key)
576
- end
577
-
578
- # Get all the values in a hash.
579
- def hvals(key)
580
- node_for(key).hvals(key)
581
- end
582
-
583
- # Get all the fields and values in a hash.
584
- def hgetall(key)
585
- node_for(key).hgetall(key)
586
- end
587
-
588
622
  # Set multiple hash fields to multiple values.
589
623
  def hmset(key, *attrs)
590
624
  node_for(key).hmset(key, *attrs)
@@ -594,6 +628,11 @@ class Redis
594
628
  node_for(key).hmset(key, *hash.to_a.flatten)
595
629
  end
596
630
 
631
+ # Get the value of a hash field.
632
+ def hget(key, field)
633
+ node_for(key).hget(key, field)
634
+ end
635
+
597
636
  # Get the values of all the given hash fields.
598
637
  def hmget(key, *fields)
599
638
  node_for(key).hmget(key, *fields)
@@ -603,6 +642,16 @@ class Redis
603
642
  Hash[*fields.zip(hmget(key, *fields)).flatten]
604
643
  end
605
644
 
645
+ # Delete one or more hash fields.
646
+ def hdel(key, field)
647
+ node_for(key).hdel(key, field)
648
+ end
649
+
650
+ # Determine if a hash field exists.
651
+ def hexists(key, field)
652
+ node_for(key).hexists(key, field)
653
+ end
654
+
606
655
  # Increment the integer value of a hash field by the given integer number.
607
656
  def hincrby(key, field, increment)
608
657
  node_for(key).hincrby(key, field, increment)
@@ -613,38 +662,19 @@ class Redis
613
662
  node_for(key).hincrbyfloat(key, field, increment)
614
663
  end
615
664
 
616
- # Sort the elements in a list, set or sorted set.
617
- def sort(key, options = {})
618
- keys = [key, options[:by], options[:store], *Array(options[:get])].compact
619
-
620
- ensure_same_node(:sort, keys) do |node|
621
- node.sort(key, options)
622
- end
623
- end
624
-
625
- # Mark the start of a transaction block.
626
- def multi
627
- raise CannotDistribute, :multi
628
- end
629
-
630
- # Watch the given keys to determine execution of the MULTI/EXEC block.
631
- def watch(*keys)
632
- raise CannotDistribute, :watch
633
- end
634
-
635
- # Forget about all watched keys.
636
- def unwatch
637
- raise CannotDistribute, :unwatch
665
+ # Get all the fields in a hash.
666
+ def hkeys(key)
667
+ node_for(key).hkeys(key)
638
668
  end
639
669
 
640
- # Execute all commands issued after MULTI.
641
- def exec
642
- raise CannotDistribute, :exec
670
+ # Get all the values in a hash.
671
+ def hvals(key)
672
+ node_for(key).hvals(key)
643
673
  end
644
674
 
645
- # Discard all commands issued after MULTI.
646
- def discard
647
- raise CannotDistribute, :discard
675
+ # Get all the fields and values in a hash.
676
+ def hgetall(key)
677
+ node_for(key).hgetall(key)
648
678
  end
649
679
 
650
680
  # Post a message to a channel.
@@ -656,12 +686,6 @@ class Redis
656
686
  !! @subscribed_node
657
687
  end
658
688
 
659
- # Stop listening for messages posted to the given channels.
660
- def unsubscribe(*channels)
661
- raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed?
662
- @subscribed_node.unsubscribe(*channels)
663
- end
664
-
665
689
  # Listen for messages published to the given channels.
666
690
  def subscribe(channel, *channels, &block)
667
691
  if channels.empty?
@@ -675,54 +699,78 @@ class Redis
675
699
  end
676
700
  end
677
701
 
702
+ # Stop listening for messages posted to the given channels.
703
+ def unsubscribe(*channels)
704
+ raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed?
705
+ @subscribed_node.unsubscribe(*channels)
706
+ end
707
+
708
+ # Listen for messages published to channels matching the given patterns.
709
+ def psubscribe(*channels, &block)
710
+ raise NotImplementedError
711
+ end
712
+
678
713
  # Stop listening for messages posted to channels matching the given
679
714
  # patterns.
680
715
  def punsubscribe(*channels)
681
716
  raise NotImplementedError
682
717
  end
683
718
 
684
- # Listen for messages published to channels matching the given patterns.
685
- def psubscribe(*channels, &block)
686
- raise NotImplementedError
719
+ # Watch the given keys to determine execution of the MULTI/EXEC block.
720
+ def watch(*keys)
721
+ raise CannotDistribute, :watch
687
722
  end
688
723
 
689
- # Synchronously save the dataset to disk.
690
- def save
691
- on_each_node :save
724
+ # Forget about all watched keys.
725
+ def unwatch
726
+ raise CannotDistribute, :unwatch
692
727
  end
693
728
 
694
- # Asynchronously save the dataset to disk.
695
- def bgsave
696
- on_each_node :bgsave
729
+ def pipelined
730
+ raise CannotDistribute, :pipelined
697
731
  end
698
732
 
699
- # Get the UNIX time stamp of the last successful save to disk.
700
- def lastsave
701
- on_each_node :lastsave
733
+ # Mark the start of a transaction block.
734
+ def multi
735
+ raise CannotDistribute, :multi
702
736
  end
703
737
 
704
- # Get information and statistics about the server.
705
- def info(cmd = nil)
706
- on_each_node :info, cmd
738
+ # Execute all commands issued after MULTI.
739
+ def exec
740
+ raise CannotDistribute, :exec
707
741
  end
708
742
 
709
- # Listen for all requests received by the server in real time.
710
- def monitor
711
- raise NotImplementedError
743
+ # Discard all commands issued after MULTI.
744
+ def discard
745
+ raise CannotDistribute, :discard
712
746
  end
713
747
 
714
- # Echo the given string.
715
- def echo(value)
716
- on_each_node :echo, value
748
+ # Control remote script registry.
749
+ def script(subcommand, *args)
750
+ on_each_node(:script, subcommand, *args)
717
751
  end
718
752
 
719
- # Get server time: an UNIX timestamp and the elapsed microseconds in the current second.
720
- def time
721
- on_each_node :time
753
+ def _eval(cmd, args)
754
+ script = args.shift
755
+ options = args.pop if args.last.is_a?(Hash)
756
+ options ||= {}
757
+
758
+ keys = args.shift || options[:keys] || []
759
+ argv = args.shift || options[:argv] || []
760
+
761
+ ensure_same_node(cmd, keys) do |node|
762
+ node.send(cmd, script, keys, argv)
763
+ end
722
764
  end
723
765
 
724
- def pipelined
725
- raise CannotDistribute, :pipelined
766
+ # Evaluate Lua script.
767
+ def eval(*args)
768
+ _eval(:eval, args)
769
+ end
770
+
771
+ # Evaluate Lua script by its SHA.
772
+ def evalsha(*args)
773
+ _eval(:evalsha, args)
726
774
  end
727
775
 
728
776
  def inspect
@@ -746,9 +794,18 @@ class Redis
746
794
  end
747
795
 
748
796
  def ensure_same_node(command, keys)
749
- tags = keys.map { |key| key_tag(key) }
797
+ all = true
798
+
799
+ tags = keys.map do |key|
800
+ tag = key_tag(key)
801
+ all = false unless tag
802
+ tag
803
+ end
750
804
 
751
- raise CannotDistribute, command if !tags.all? || tags.uniq.size != 1
805
+ if (all && tags.uniq.size != 1) || (!all && keys.uniq.size != 1)
806
+ # Not 1 unique tag or not 1 unique key
807
+ raise CannotDistribute, command
808
+ end
752
809
 
753
810
  yield(node_for(keys.first))
754
811
  end