oxblood 0.1.0.dev8 → 0.1.0.dev9

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.
@@ -1,5 +1,9 @@
1
+ require 'oxblood/commands'
2
+ require 'oxblood/protocol'
3
+
1
4
  module Oxblood
2
- # Implements usual Request/Response protocol
5
+ # Implements usual Request/Response protocol.
6
+ # Error responses will be raised.
3
7
  #
4
8
  # @note {Session} don't maintain threadsafety! In multithreaded environment
5
9
  # please use {Pool}
@@ -9,1378 +13,19 @@ module Oxblood
9
13
  # session = Oxblood::Session.new(conn)
10
14
  # session.ping # => 'PONG'
11
15
  class Session
16
+ include Oxblood::Commands
17
+
12
18
  def initialize(connection)
13
19
  @connection = connection
14
20
  end
15
21
 
16
- #
17
- # Hashes
18
- #
19
-
20
- # Removes the specified fields from the hash stored at key
21
- # @see http://redis.io/commands/hdel
22
- #
23
- # @param [String] key under which hash is stored
24
- # @param [Array<#to_s>] fields to delete
25
- #
26
- # @return [Integer] the number of fields that were removed from the hash
27
- def hdel(key, fields)
28
- run(:HDEL, key, fields)
29
- end
30
-
31
- # Returns if field is an existing field in the hash stored at key
32
- # @see http://redis.io/commands/hexists
33
- #
34
- # @param [String] key under which hash is stored
35
- # @param [String] field to check for existence
36
- #
37
- # @return [Boolean] do hash contains field or not
38
- def hexists(key, field)
39
- 1 == run(:HEXISTS, key, field)
40
- end
41
-
42
- # Get the value of a hash field
43
- # @see http://redis.io/commands/hget
44
- #
45
- # @param [String] key under which hash is stored
46
- # @param [String] field name
47
- #
48
- # @return [String, nil] the value associated with field
49
- # or nil when field is not present in the hash or key does not exist.
50
- def hget(key, field)
51
- run(:HGET, key, field)
52
- end
53
-
54
- # Get all the fields and values in a hash
55
- # @see http://redis.io/commands/hgetall
56
- #
57
- # @param [String] key under which hash is stored
58
- #
59
- # @return [Hash] of fields and their values
60
- def hgetall(key)
61
- Hash[*run(:HGETALL, key)]
62
- end
63
-
64
- # Increment the integer value of a hash field by the given number
65
- # @see http://redis.io/commands/hincrby
66
- #
67
- # @param [String] key under which hash is stored
68
- # @param [String] field to increment
69
- # @param [Integer] increment by value
70
- #
71
- # @return [Integer] the value at field after the increment operation
72
- def hincrby(key, field, increment)
73
- run(:HINCRBY, key, field, increment)
74
- end
75
-
76
- # Increment the float value of a hash field by the given number
77
- # @see http://redis.io/commands/hincrby
78
- #
79
- # @param [String] key under which hash is stored
80
- # @param [String] field to increment
81
- # @param [Integer] increment by value
82
- #
83
- # @return [String] the value of field after the increment
84
- # @return [RError] field contains a value of the wrong type (not a string).
85
- # Or the current field content or the specified increment are not parsable
86
- # as a double precision floating point number.
87
- def hincrbyfloat(key, field, increment)
88
- run(:HINCRBYFLOAT, key, field, increment)
89
- end
90
-
91
- # Get all the keys in a hash
92
- # @see http://redis.io/commands/hkeys
93
- #
94
- # @param [String] key
95
- #
96
- # @return [Array] list of fields in the hash, or an empty list when
97
- # key does not exist.
98
- def hkeys(key)
99
- run(:HKEYS, key)
100
- end
101
-
102
- # Get the number of keys in a hash
103
- # @see http://redis.io/commands/hlen
104
- #
105
- # @param [String] key
106
- #
107
- # @return [Integer] number of fields in the hash, or 0 when
108
- # key does not exist.
109
- def hlen(key)
110
- run(:HLEN, key)
111
- end
112
-
113
- # Get the field values of all given hash fields
114
- # @see http://redis.io/commands/hmget
115
- #
116
- # @param [String] key under which hash is stored
117
- # @param [String, Array<String>] fields to get
118
- #
119
- # @return [Array] list of values associated with the given fields,
120
- # in the same order as they are requested.
121
- def hmget(key, *fields)
122
- run(*fields.unshift(:HMGET, key))
123
- end
124
-
125
- # Set multiple hash fields to multiple values
126
- # @see http://redis.io/commands/hmset
127
- #
128
- # @param [String] key under which store hash
129
- # @param [[String, String], Array<[String, String]>] args fields and values
130
- #
131
- # @return [String] 'OK'
132
- def hmset(key, *args)
133
- run(*args.unshift(:HMSET, key))
134
- end
135
-
136
-
137
- # Set the string value of a hash field
138
- # @see http://redis.io/commands/hset
139
- #
140
- # @param [String] key
141
- # @param [String] field
142
- # @param [String] value
143
- #
144
- # @return [Integer] 1 if field is a new field in the hash and value was set.
145
- # 0 if field already exists in the hash and the value was updated.
146
- def hset(key, field, value)
147
- run(:HSET, key, field, value)
148
- end
149
-
150
- # Set the value of a hash field, only if the field does not exist
151
- # @see http://redis.io/commands/hsetnx
152
- #
153
- # @param [String] key
154
- # @param [String] field
155
- # @param [String] value
156
- #
157
- # @return [Integer] 1 if field is a new field in the hash and value was set.
158
- # 0 if field already exists in the hash and no operation was performed.
159
- def hsetnx(key, field, value)
160
- run(:HSETNX, key, field, value)
161
- end
162
-
163
- # Get the length of the value of a hash field
164
- # @see http://redis.io/commands/hstrlen
165
- #
166
- # @param [String] key
167
- # @param [String] field
168
- #
169
- # @return [Integer] the string length of the value associated with field,
170
- # or 0 when field is not present in the hash or key does not exist at all.
171
- def hstrlen(key, field)
172
- run(:HSTRLEN, key, field)
173
- end
174
-
175
- # Get all values in a hash
176
- # @see http://redis.io/commands/hvals
177
- #
178
- # @param [String] key
179
- #
180
- # @return [Array] list of values in the hash, or an empty list when
181
- # key does not exist
182
- def hvals(key)
183
- run(:HVALS, key)
184
- end
185
-
186
- #
187
- # Strings
188
- #
189
-
190
- # Append a value to a key
191
- # @see http://redis.io/commands/append
192
- #
193
- # @param [String] key
194
- # @param [String] value
195
- #
196
- # @return [Integer] the length of the string after the append operation
197
- def append(key, value)
198
- run(:APPEND, key, value)
199
- end
200
-
201
- # Count set bits in a string
202
- # @see http://redis.io/commands/bitcount
203
- #
204
- # @param [String] key
205
- # @param [Array] interval to count in
206
- #
207
- # @return [Integer] the number of bits set to 1
208
- def bitcount(key, *interval)
209
- run(*interval.unshift(:BITCOUNT, key))
210
- end
211
-
212
- # Perform bitwise operations between strings
213
- # @see http://redis.io/commands/bitop
214
- #
215
- # @param [String] operation
216
- # @param [String] destkey
217
- # @param [Array] keys
218
- #
219
- # @return [Integer] the size of the string stored in the destination key,
220
- # that is equal to the size of the longest input string
221
- def bitop(operation, destkey, *keys)
222
- run(*keys.unshift(:BITOP, operation, destkey))
223
- end
224
-
225
- # Find first bit set or clear in a string
226
- # @see http://redis.io/commands/bitpos
227
- #
228
- # @param [String] key
229
- # @param [Integer] bit
230
- # @param [Array] interval
231
- #
232
- # @return [Integer] the command returns the position of the first bit set to
233
- # 1 or 0 according to the request
234
- def bitpos(key, bit, *interval)
235
- run(*interval.unshift(:BITPOS, key, bit))
236
- end
237
-
238
- # Decrement the integer value of a key by one
239
- # @see http://redis.io/commands/decr
240
- #
241
- # @param [String] key
242
- #
243
- # @return [Integer] the value of key after the decrement
244
- # @return [RError] if value is not an integer or out of range
245
- def decr(key)
246
- run(:DECR, key)
247
- end
248
-
249
- # Decrement the integer value of a key by the given number
250
- # @see http://redis.io/commands/decrby
251
- #
252
- # @param [String] key
253
- # @param [Integer] decrement
254
- #
255
- # @return [Integer] the value of key after the decrement
256
- # @return [RError] if the key contains a value of the wrong type or contains
257
- # a string that can not be represented as integer
258
- def decrby(key, decrement)
259
- run(:DECRBY, key, decrement)
260
- end
261
-
262
- # Get the value of a key
263
- # @see http://redis.io/commands/get
264
- #
265
- # @param [String] key
266
- #
267
- # @return [String, nil] the value of key, or nil when key does not exists
268
- def get(key)
269
- run(:GET, key)
270
- end
271
-
272
- # Returns the bit value at offset in the string value stored at key
273
- # @see http://redis.io/commands/getbit
274
- #
275
- # @param [String] key
276
- # @param [Integer] offset
277
- #
278
- # @return [Integer] the bit value stored at offset
279
- def getbit(key, offset)
280
- run(:GETBIT, key, offset)
281
- end
282
-
283
- # Get a substring of the string stored at a key
284
- # @see http://redis.io/commands/getrange
285
- #
286
- # @param [String] key
287
- # @param [Integer] start_pos
288
- # @param [Integer] end_pos
289
- #
290
- # @return [String] substring
291
- def getrange(key, start_pos, end_pos)
292
- run(:GETRANGE, key, start_pos, end_pos)
293
- end
294
-
295
- # Set the string value of a key and return its old value
296
- # @see http://redis.io/commands/getset
297
- #
298
- # @param [String] key
299
- # @param [String] value
300
- #
301
- # @return [String, nil] the old value stored at key, or nil when
302
- # key did not exist
303
- def getset(key, value)
304
- run(:GETSET, key, value)
305
- end
306
-
307
- # Increment the integer value of a key by one
308
- # @see http://redis.io/commands/incr
309
- #
310
- # @param [String] key
311
- #
312
- # @return [Integer] the value of key after the increment
313
- # @return [RError] if the key contains a value of the wrong type or contains
314
- # a string that can not be represented as integer
315
- def incr(key)
316
- run(:INCR, key)
317
- end
318
-
319
- # Increment the integer value of a key by the given amount
320
- # @see http://redis.io/commands/incrby
321
- #
322
- # @param [String] key
323
- # @param [Integer] increment
324
- #
325
- # @return [Integer] the value of key after the increment
326
- def incrby(key, increment)
327
- run(:INCRBY, key, increment)
328
- end
329
-
330
- # Increment the float value of a key by the given amount
331
- # @see http://redis.io/commands/incrbyfloat
332
- #
333
- # @param [String] key
334
- # @param [Float] increment
335
- #
336
- # @return [String] the value of key after the increment
337
- def incrbyfloat(key, increment)
338
- run(:INCRBYFLOAT, key, increment)
339
- end
340
-
341
- # Get the values of all the given keys
342
- # @see http://redis.io/commands/mget
343
- #
344
- # @param [Array<String>] keys to retrieve
345
- #
346
- # @return [Array] list of values at the specified keys
347
- def mget(*keys)
348
- run(*keys.unshift(:MGET))
349
- end
350
-
351
- # Set multiple keys to multiple values
352
- # @see http://redis.io/commands/mset
353
- #
354
- # @param [Array] keys_and_values
355
- #
356
- # @return [String] 'OK'
357
- def mset(*keys_and_values)
358
- run(*keys_and_values.unshift(:MSET))
359
- end
360
-
361
- # Set multiple keys to multiple values, only if none of the keys exist
362
- # @see http://redis.io/commands/msetnx
363
- #
364
- # @param [Array] keys_and_values
365
- #
366
- # @return [Integer] 1 if the all the keys were set, or
367
- # 0 if no key was set (at least one key already existed)
368
- def msetnx(*keys_and_values)
369
- run(*keys_and_values.unshift(:MSETNX))
370
- end
371
-
372
- # Set the value and expiration in milliseconds of a key
373
- # @see http://redis.io/commands/psetex
374
- #
375
- # @param [String] key
376
- # @param [Integer] milliseconds expire time
377
- # @param [String] value
378
- #
379
- # @return [String] 'OK'
380
- def psetex(key, milliseconds, value)
381
- run(:PSETEX, key, milliseconds, value)
382
- end
383
-
384
- # Set the string value of a key
385
- # @see http://redis.io/commands/set
386
- #
387
- # @todo Add support for set options
388
- # http://redis.io/commands/set#options
389
- #
390
- # @param [String] key
391
- # @param [String] value
392
- #
393
- # @return [String] 'OK' if SET was executed correctly
394
- def set(key, value)
395
- run(:SET, key, value)
396
- end
397
-
398
- # Set or clear the bit at offset in the string value stored at key
399
- # @see http://redis.io/commands/setbit
400
- #
401
- # @param [String] key
402
- # @param [Integer] offset
403
- # @param [String] value
404
- #
405
- # @return [Integer] the original bit value stored at offset
406
- def setbit(key, offset, value)
407
- run(:SETBIT, key, offset, value)
408
- end
409
-
410
- # Set the value and expiration of a key
411
- # @see http://redis.io/commands/setex
412
- #
413
- # @param [String] key
414
- # @param [Integer] seconds expire time
415
- # @param [String] value
416
- #
417
- # @return [String] 'OK'
418
- def setex(key, seconds, value)
419
- run(:SETEX, key, seconds, value)
420
- end
421
-
422
- # Set the value of a key, only if the key does not exist
423
- # @see http://redis.io/commands/setnx
424
- #
425
- # @param [String] key
426
- # @param [String] value
427
- #
428
- # @return [Integer] 1 if the key was set, or 0 if the key was not set
429
- def setnx(key, value)
430
- run(:SETNX, key, value)
431
- end
432
-
433
- # Overwrite part of a string at key starting at the specified offset
434
- # @see http://redis.io/commands/setrange
435
- #
436
- # @param [String] key
437
- # @param [Integer] offset
438
- # @param [String] value
439
- #
440
- # @return [Integer] the length of the string after it was modified by
441
- # the command
442
- def setrange(key, offset, value)
443
- run(:SETRANGE, key, offset, value)
444
- end
445
-
446
- # Get the length of the value stored in a key
447
- # @see http://redis.io/commands/strlen
448
- #
449
- # @param [String] key
450
- #
451
- # @return [Integer] the length of the string at key,
452
- # or 0 when key does not exist
453
- def strlen(key)
454
- run(:STRLEN, key)
455
- end
456
-
457
- #
458
- # Connection
459
- #
460
-
461
- # Authenticate to the server
462
- # @see http://redis.io/commands/auth
463
- #
464
- # @param [String] password
465
- #
466
- # @return [String] 'OK'
467
- # @return [RError] if wrong password was passed or server does not require
468
- # password
469
- def auth(password)
470
- run(:AUTH, password)
471
- end
472
-
473
- # Echo the given string
474
- # @see http://redis.io/commands/echo
475
- #
476
- # @param [String] message
477
- #
478
- # @return [String] given string
479
- def echo(message)
480
- run(:ECHO, message)
481
- end
482
-
483
- # Like {#auth}, except that if error returned, raises it.
484
- #
485
- # @param [String] password
486
- #
487
- # @raise [Protocol::RError] if error returned
488
- #
489
- # @return [String] 'OK'
490
- def auth!(password)
491
- response = auth(password)
492
- error?(response) ? (raise response) : response
493
- end
494
-
495
- # Returns PONG if no argument is provided, otherwise return a copy of
496
- # the argument as a bulk
497
- # @see http://redis.io/commands/ping
498
- #
499
- # @param [String] message to return
500
- #
501
- # @return [String] message passed as argument
502
- def ping(message = nil)
503
- message ? run(:PING, message) : run(:PING)
504
- end
505
-
506
- # Change the selected database for the current connection
507
- # @see http://redis.io/commands/select
508
- #
509
- # @param [Integer] index database to switch
510
- #
511
- # @return [String] 'OK'
512
- # @return [RError] if wrong index was passed
513
- def select(index)
514
- run(:SELECT, index)
515
- end
516
-
517
- # Close the connection
518
- # @see http://redis.io/commands/quit
519
- #
520
- # @return [String] 'OK'
521
- def quit
522
- run(:QUIT)
523
- ensure
524
- @connection.socket.close
525
- end
526
-
527
- #
528
- # Server
529
- #
530
-
531
- # Remove all keys from the current database
532
- # @see http://redis.io/commands/flushdb
533
- #
534
- # @return [String] should always return 'OK'
535
- def flushdb
536
- run(:FLUSHDB)
537
- end
538
-
539
- # Returns information and statistics about the server in a format that is
540
- # simple to parse by computers and easy to read by humans
541
- # @see http://redis.io/commands/info
542
- #
543
- # @param [String] section used to select a specific section of information
544
- #
545
- # @return [String] raw redis server response as a collection of text lines.
546
- def info(section = nil)
547
- section ? run(:INFO, section) : run(:INFO)
548
- end
549
-
550
- #
551
- # Keys
552
- #
553
-
554
- # Delete a key
555
- # @see http://redis.io/commands/del
556
- #
557
- # @param [String, Array<String>] keys to delete
558
- #
559
- # @return [Integer] the number of keys that were removed
560
- def del(*keys)
561
- run(*keys.unshift(:DEL))
562
- end
563
-
564
- # Return a serialized version of the value stored at specified key.
565
- # @see http://redis.io/commands/dump
566
- #
567
- # @param [String] key
568
- #
569
- # @return [String] serialized value
570
- def dump(key)
571
- run(:DUMP, key)
572
- end
573
-
574
- # Determine if a key exists
575
- # @see http://redis.io/commands/exists
576
- #
577
- # @param [String, Array<String>] keys to check
578
- #
579
- # @return [Integer] the number of keys existing among the ones specified as
580
- # arguments. Keys mentioned multiple times and existing are counted
581
- # multiple times.
582
- def exists(*keys)
583
- run(*keys.unshift(:EXISTS))
584
- end
585
-
586
- # Set a key's time to live in seconds
587
- # @see http://redis.io/commands/expire
588
- #
589
- # @param [String] key to expire
590
- # @param [Integer] seconds number of seconds
591
- #
592
- # @return [Integer] 1 if the timeout was set. 0 if key does not exist or
593
- # the timeout could not be set.
594
- def expire(key, seconds)
595
- run(:EXPIRE, key, seconds)
596
- end
597
-
598
- # Set the expiration for a key as a UNIX timestamp
599
- # @see http://redis.io/commands/expireat
600
- #
601
- # @param [String] key
602
- # @param [Integer] timestamp in UNIX format
603
- #
604
- # @return [Integer] 1 if the timeout was set. 0 if key does not exist or
605
- # the timeout could not be set.
606
- def expireat(key, timestamp)
607
- run(:EXPIREAT, key, timestamp)
608
- end
609
-
610
- # Find all keys matching the given pattern
611
- # @see http://redis.io/commands/keys
612
- #
613
- # @param [String] pattern used to match keys
614
- def keys(pattern)
615
- run(:KEYS, pattern)
616
- end
617
-
618
- # Move a key to another database
619
- # @see http://redis.io/commands/move
620
- #
621
- # @param [String] key
622
- # @param [Integer] db index
623
- #
624
- # @return [Integer] 1 if key was moved and 0 otherwise.
625
- def move(key, db)
626
- run(:MOVE, key, db)
627
- end
628
-
629
- # Inspect the internals of Redis objects
630
- # @see http://redis.io/commands/object
631
- #
632
- # @param [String] subcommand `REFCOUNT`, `ENCODING`, `IDLETIME`
633
- # @param [String] key
634
- #
635
- # @return [Integer] in case of `REFCOUNT` and `IDLETIME` subcommands
636
- # @return [String] in case of `ENCODING` subcommand
637
- # @return [nil] if object you try to inspect is missing
638
- def object(subcommand, key)
639
- run(:OBJECT, subcommand, key)
640
- end
641
-
642
- # Remove expiration from a key
643
- # @see http://redis.io/commands/persist
644
- # @param [String] key
645
- #
646
- # @return [Integer] 1 if the timeout was removed and 0 otherwise
647
- def persist(key)
648
- run(:PERSIST, key)
649
- end
650
-
651
- # Set a key's time to live in milliseconds
652
- # @see http://redis.io/commands/pexpire
653
- #
654
- # @param [String] key
655
- # @param [Integer] milliseconds
656
- #
657
- # @return [Integer] 1 if the timeout was set and 0 otherwise
658
- def pexpire(key, milliseconds)
659
- run(:PEXPIRE, key, milliseconds)
660
- end
661
-
662
- # Set the expiration for a key as a UNIX timestamp specified in milliseconds
663
- # @see http://redis.io/commands/pexpireat
664
- #
665
- # @param [String] key
666
- # @param [Integer] timestamp in milliseconds
667
- #
668
- # @return [Integer] 1 if the timeout was set and 0 otherwise
669
- def pexpireat(key, timestamp)
670
- run(:PEXPIREAT, key, timestamp)
671
- end
672
-
673
- # Get the time to live for a key in milliseconds
674
- # @see http://redis.io/commands/pttl
675
- #
676
- # @param [String] key
677
- #
678
- # @return [Integer] TTL in milliseconds, or a negative value in order to
679
- # signal an error
680
- def pttl(key)
681
- run(:PTTL, key)
682
- end
683
-
684
- # Return a random key from the keyspace
685
- # @see http://redis.io/commands/randomkey
686
- #
687
- # @return [String] the random key
688
- # @return [nil] if database is empty
689
- def randomkey
690
- run(:RANDOMKEY)
691
- end
692
-
693
- # Rename a key
694
- # @see http://redis.io/commands/rename
695
- #
696
- # @param [String] key to rename
697
- # @param [String] newkey
698
- #
699
- # @return [String] OK in case of success
700
- # @return [RError] if key does not exist. Before Redis 3.2.0, an error is
701
- # returned if source and destination names are the same.
702
- def rename(key, newkey)
703
- run(:RENAME, key, newkey)
704
- end
705
-
706
- # Rename a key, only if the new key does not exist
707
- # @see http://redis.io/commands/renamenx
708
- #
709
- # @param [String] key to rename
710
- # @param [String] newkey
711
- #
712
- # @return [Integer] 1 if key was renamed to newkey. 0 if newkey already
713
- # exists.
714
- # @return [RError] if key does not exist. Before Redis 3.2.0, an error is
715
- # returned if source and destination names are the same.
716
- def renamenx(key, newkey)
717
- run(:RENAMENX, key, newkey)
718
- end
719
-
720
- # Create a key using the provided serialized value, previously obtained
721
- # using DUMP
722
- # @see http://redis.io/commands/restore
723
- #
724
- # @param [String] key
725
- # @param [Integer] ttl expire time in milliseconds
726
- # @param [String] serialized_value obtained using DUMP command
727
- # @param [Hash] opts
728
- #
729
- # @option opts [Boolean] :replace (false) Override key if it already exists
730
- #
731
- # @return [String] OK on success
732
- # @return [RError] if replace is false and key already exists or RDB version
733
- # and data checksum don't match.
734
- def restore(key, ttl, serialized_value, opts = {})
735
- args = [:RESTORE, key, ttl, serialized_value]
736
- args << :REPLACE if opts[:replace]
737
-
738
- run(*args)
739
- end
740
-
741
- # Get the time to live for a key
742
- # @see http://redis.io/commands/ttl
743
- #
744
- # @param [String] key
745
- #
746
- # @return [Integer] TTL in seconds, or a negative value in order to signal
747
- # an error
748
- def ttl(key)
749
- run(:TTL, key)
750
- end
751
-
752
- # Determine the type stored at key
753
- # @see http://redis.io/commands/type
754
- #
755
- # @param [String] key
756
- #
757
- # @return [String] type of key, or none when key does not exist.
758
- def type(key)
759
- run(:TYPE, key)
760
- end
761
-
762
- #
763
- # Lists
764
- #
765
-
766
- # Get an element from a list by its index
767
- # @see http://www.redis.io/commands/lindex
768
- #
769
- # @param [String] key
770
- # @param [Integer] index zero-based of element in the list
771
- #
772
- # @return [String] the requested element, or nil when index is out of range.
773
- def lindex(key, index)
774
- run(:LINDEX, key, index)
775
- end
776
-
777
- # Insert an element before or after another element in a list
778
- # @see http://www.redis.io/commands/linsert
779
- #
780
- # @param [String] key
781
- # @param [Symbol] place could be :before or :after
782
- # @param [String] pivot reference value
783
- # @param [String] value to insert
784
- #
785
- # @return [Integer] the length of the list after the insert operation,
786
- # or -1 when the value pivot was not found
787
- def linsert(key, place, pivot, value)
788
- run(:LINSERT, key, place, pivot, value)
789
- end
790
-
791
- # Get the length of a list
792
- # @see http://redis.io/commands/llen
793
- #
794
- # @param [String] key
795
- #
796
- # @return [Integer] the length of the list at key
797
- # @return [RError] if the value stored at key is not a list
798
- def llen(key)
799
- run(:LLEN, key)
800
- end
801
-
802
- # Remove and get the first element in a list
803
- # @see http://redis.io/commands/lpop
804
- #
805
- # @param [String] key
806
- #
807
- # @return [String, nil] the value of the first element,
808
- # or nil when key does not exist.
809
- def lpop(key)
810
- run(:LPOP, key)
811
- end
812
-
813
- # Prepend one or multiple values to a list
814
- # @see http://redis.io/commands/lpush
815
- #
816
- # @param [String] key
817
- # @param [Array] values to prepend
818
- #
819
- # @return [Integer] the length of the list after the push operations
820
- def lpush(key, *values)
821
- run(*values.unshift(:LPUSH, key))
822
- end
823
-
824
- # Prepend a value to a list, only if the list exists
825
- # @see http://www.redis.io/commands/lpushx
826
- #
827
- # @param [String] key
828
- # @param [String] value
829
- #
830
- # @return [Integer] the length of the list after the push operation
831
- def lpushx(key, value)
832
- run(:LPUSHX, key, value)
833
- end
834
-
835
- # Get a range of elements from a list
836
- # @see http://redis.io/commands/lrange
837
- #
838
- # @param [String] key
839
- # @param [Integer] start index
840
- # @param [Integer] stop index
841
- #
842
- # @return [Array] list of elements in the specified range
843
- def lrange(key, start, stop)
844
- run(:LRANGE, key, start, stop)
845
- end
846
-
847
- # Remove elements from a list
848
- # @see http://www.redis.io/commands/lrem
849
- #
850
- # @param [String] key
851
- # @param [Integer] count (please look into official docs for more info)
852
- # @param [String] value to remove
853
- #
854
- # @return [Integer] the number of removed elements
855
- def lrem(key, count, value)
856
- run(:LREM, key, count, value)
857
- end
858
-
859
- # Set the value of an element in a list by its index
860
- # @see http://www.redis.io/commands/lset
861
- #
862
- # @param [String] key
863
- # @param [Integer] index
864
- # @param [String] value
865
- #
866
- # @return [String] 'OK'
867
- # @return [RError] if index is out of range
868
- def lset(key, index, value)
869
- run(:LSET, key, index, value)
870
- end
871
-
872
- # Trim a list to the specified range
873
- # @see http://www.redis.io/commands/ltrim
874
- #
875
- # @param [String] key
876
- # @param [Integer] start
877
- # @param [Integer] stop
878
- #
879
- # @return [String] 'OK'
880
- def ltrim(key, start, stop)
881
- run(:LTRIM, key, start, stop)
882
- end
883
-
884
- # Remove and get the last element in a list
885
- # @see http://redis.io/commands/rpop
886
- #
887
- # @param [String] key
888
- #
889
- # @return [String, nil] the value of the last element, or nil when key does
890
- # not exist
891
- def rpop(key)
892
- run(:RPOP, key)
893
- end
894
-
895
- # Remove the last element in a list, prepend it to another list and return
896
- # @see http://www.redis.io/commands/rpoplpush
897
- #
898
- # @param [String] source
899
- # @param [String] destination
900
- #
901
- # @return [String, nil] the element being popped and pushed, or nil when
902
- # source does not exist
903
- def rpoplpush(source, destination)
904
- run(:RPOPLPUSH, source, destination)
905
- end
906
-
907
- # Append one or multiple values to a list
908
- # @see http://redis.io/commands/rpush
909
- #
910
- # @param [String] key
911
- # @param [Array] values to add
912
- #
913
- # @return [Integer] the length of the list after the push operation
914
- # @return [RError] if key holds a value that is not a list
915
- def rpush(key, *values)
916
- run(*values.unshift(:RPUSH, key))
917
- end
918
-
919
- # Append a value to a list, only if the list exists
920
- # @see http://www.redis.io/commands/rpushx
921
- #
922
- # @param [String] key
923
- # @param [String] value
924
- #
925
- # @return [Integer] the length of the list after the push operation
926
- def rpushx(key, value)
927
- run(:RPUSHX, key, value)
928
- end
929
-
930
- #
931
- # Sets
932
- #
933
-
934
- # Add one or more members to a set
935
- # @see http://redis.io/commands/sadd
936
- #
937
- # @param [String] key under which store set
938
- # @param [String, Array<String>] members to store
939
- #
940
- # @return [Integer] the number of elements that were added to the set,
941
- # not including all the elements already present into the set.
942
- def sadd(key, *members)
943
- run(*members.unshift(:SADD, key))
944
- end
945
-
946
- # Get the number of members in a set
947
- # @see http://redis.io/commands/scard
948
- #
949
- # @param [String] key
950
- #
951
- # @return [Integer] the cardinality (number of elements) of the set, or 0 if
952
- # key does not exist
953
- def scard(key)
954
- run(:SCARD, key)
955
- end
956
-
957
- # Subtract multiple sets
958
- # @see http://redis.io/commands/sdiff
959
- #
960
- # @param [String, Array<String>] keys
961
- #
962
- # @return [Array] array with members of the resulting set
963
- def sdiff(*keys)
964
- run(*keys.unshift(:SDIFF))
965
- end
966
-
967
- # Subtract multiple sets and store the resulting set in a key
968
- # @see http://redis.io/commands/sdiffstore
969
- #
970
- # @param [String] destination key
971
- # @param [String, Array<String>] keys of sets to diff
972
- #
973
- # @return [Integer] the number of elements in the resulting set
974
- def sdiffstore(destination, *keys)
975
- run(*keys.unshift(:SDIFFSTORE, destination))
976
- end
977
-
978
- # Intersect multiple sets
979
- # @see http://redis.io/commands/sinter
980
- #
981
- # @param [String, Array<String>] keys to intersect
982
- #
983
- # @return [Array] array with members of the resulting set
984
- def sinter(*keys)
985
- run(*keys.unshift(:SINTER))
986
- end
987
-
988
- # Intersect multiple sets and store the resulting key in a key
989
- # @see http://redis.io/commands/sinterstore
990
- #
991
- # @param [String] destination key
992
- # @param [String, Array<String>] keys of sets to intersect
993
- #
994
- # @return [Integer] the number of elements in the resulting set
995
- def sinterstore(destination, *keys)
996
- run(*keys.unshift(:SINTERSTORE, destination))
997
- end
998
-
999
- # Determine if a given value is a member of a set
1000
- # @see http://redis.io/commands/sismember
1001
- #
1002
- # @param [String] key
1003
- # @param [String] member
1004
- #
1005
- # @return [Integer] 1 if the element is a member of the set or
1006
- # 0 if the element is not a member of the set, or if key does not exist
1007
- def sismember(key, member)
1008
- run(:SISMEMBER, key, member)
1009
- end
1010
-
1011
- # Get all the members in a set
1012
- # @see http://redis.io/commands/smembers
1013
- #
1014
- # @param [String] key
1015
- #
1016
- # @return [Array] all elements of the set
1017
- def smembers(key)
1018
- run(:SMEMBERS, key)
1019
- end
1020
-
1021
- # Move a member from one set to another
1022
- # @see http://redis.io/commands/smove
1023
- #
1024
- # @param [String] source
1025
- # @param [String] destination
1026
- # @param [String] member
1027
- #
1028
- # @return [Integer] 1 if the element is moved, or 0 if the element is not
1029
- # a member of source and no operation was performed
1030
- def smove(source, destination, member)
1031
- run(:SMOVE, source, destination, member)
1032
- end
1033
-
1034
- # Remove and return one or multiple random members from a set
1035
- # @see http://redis.io/commands/spop
1036
- #
1037
- # @param [String] key
1038
- # @param [Integer] count
1039
- #
1040
- # @return [String] without the additional count argument the command returns
1041
- # the removed element, or nil when key does not exist
1042
- # @return [Array] when the additional count argument is passed the command
1043
- # returns an array of removed elements, or an empty array when key does
1044
- # not exist.
1045
- def spop(key, count = nil)
1046
- args = [:SPOP, key]
1047
- args << count if count
1048
- run(*args)
1049
- end
1050
-
1051
- # Get one or multiple random members from a set
1052
- # @see http://redis.io/commands/srandmember
1053
- #
1054
- # @param [String] key
1055
- # @param [Integer] count
1056
- #
1057
- # @return [String, nil] without the additional count argument the command
1058
- # returns string with the randomly selected element, or nil when key
1059
- # does not exist
1060
- # @return [Array] when the additional count argument is passed the command
1061
- # returns an array of elements, or an empty array when key does not exist
1062
- def srandmember(key, count = nil)
1063
- args = [:SRANDMEMBER, key]
1064
- args << count if count
1065
- run(*args)
1066
- end
1067
-
1068
- # Remove one or more members from a set
1069
- # @see http://redis.io/commands/srem
1070
- #
1071
- # @param [String] key
1072
- # @param [Array] members to remove
1073
- #
1074
- # @return [Integer] the number of members that were removed from the set,
1075
- # not including non existing members
1076
- def srem(key, *members)
1077
- run(*members.unshift(:SREM, key))
1078
- end
1079
-
1080
- # Add multiple sets
1081
- # @see http://redis.io/commands/sunion
1082
- #
1083
- # @param [String, Array<String>] keys
1084
- #
1085
- # @return [Array] list with members of the resulting set
1086
- def sunion(*keys)
1087
- run(*keys.unshift(:SUNION))
1088
- end
1089
-
1090
- # Add multipe sets and store the resulting set in a key
1091
- # @see http://redis.io/commands/sunionstore
1092
- #
1093
- # @param [String] destination
1094
- # @param [String, Array<String>] keys
1095
- #
1096
- # @return [Integer] the number of elements in the resulting set
1097
- def sunionstore(destination, *keys)
1098
- run(*keys.unshift(:SUNIONSTORE, destination))
1099
- end
1100
-
1101
- #
1102
- # Sorted Sets
1103
- #
1104
-
1105
- # Add one or more members to a sorted set, or update its score if it already
1106
- # exists.
1107
- # @see http://redis.io/commands/zadd
1108
- #
1109
- # @todo Add support for zadd options
1110
- # http://redis.io/commands/zadd#zadd-options-redis-302-or-greater
1111
- #
1112
- # @param [String] key under which store set
1113
- # @param [[Float, String], Array<[Float, String]>] args scores and members
1114
- #
1115
- # @return [Integer] The number of elements added to the sorted sets, not
1116
- # including elements already existing for which the score was updated
1117
- def zadd(key, *args)
1118
- run(*args.unshift(:ZADD, key))
1119
- end
1120
-
1121
- # Get the number of members in a sorted set
1122
- # @see http://redis.io/commands/zcard
1123
- #
1124
- # @param [String] key
1125
- #
1126
- # @return [Integer] the cardinality (number of elements) of the sorted set,
1127
- # or 0 if key does not exists
1128
- def zcard(key)
1129
- run(:ZCARD, key)
1130
- end
1131
-
1132
- # Count the members in a sorted set with scores within the given values
1133
- # @see http://redis.io/commands/zcount
1134
- #
1135
- # @param [String] key
1136
- # @param [String] min
1137
- # @param [String] max
1138
- #
1139
- # @return [Integer] the number of elements in the specified score range
1140
- def zcount(key, min, max)
1141
- run(:ZCOUNT, key, min, max)
1142
- end
1143
-
1144
- # Increment the score of a member in a sorted set
1145
- # @see http://redis.io/commands/zincrby
1146
- #
1147
- # @param [String] key
1148
- # @param [Float] increment
1149
- # @param [String] member
1150
- #
1151
- # @return [String] the new score of member (a double precision floating
1152
- # point number), represented as string
1153
- def zincrby(key, increment, member)
1154
- run(:ZINCRBY, key, increment, member)
1155
- end
1156
-
1157
- # Count the number of members in a sorted set between a given
1158
- # lexicographical range
1159
- # @see http://redis.io/commands/zlexcount
1160
- #
1161
- # @param [String] key
1162
- # @param [String] min
1163
- # @param [String] max
1164
- #
1165
- # @return the number of elements in the specified score range
1166
- def zlexcount(key, min, max)
1167
- run(:ZLEXCOUNT, key, min, max)
1168
- end
1169
-
1170
- # Return a range of members in a sorted set, by index
1171
- # @see http://redis.io/commands/zrange
1172
- #
1173
- # @example
1174
- # session.zrange('myzset', 0, -1)
1175
- # # => ['one', 'two']
1176
- #
1177
- # @example
1178
- # session.zrange('myzset', 0, -1, withscores: true)
1179
- # # => [['one', '1'], ['two', '2']]
1180
- #
1181
- # @param [String] key
1182
- # @param [Integer] start index
1183
- # @param [Integer] stop index
1184
- # @param [Hash] opts
1185
- #
1186
- # @option opts [Boolean] :withscores (false) Return the scores of
1187
- # the elements together with the elements
1188
- #
1189
- # @return [Array] list of elements in the specified range (optionally with
1190
- # their scores, in case the :withscores option is given)
1191
- def zrange(key, start, stop, opts = {})
1192
- args = [:ZRANGE, key, start, stop]
1193
- args << :WITHSCORES if opts[:withscores]
1194
- run(*args)
1195
- end
1196
-
1197
- # Return a range of members in a sorted set, by score
1198
- # @see http://redis.io/commands/zrangebyscore
1199
- #
1200
- # @param [String] key under which set is stored
1201
- # @param [String] min score
1202
- # @param [String] max score
1203
- # @param [Hash] opts
1204
- #
1205
- # @option opts [Boolean] :withscores (false) Return the scores of
1206
- # the elements together with the elements
1207
- # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
1208
- # elements (similar to SELECT LIMIT offset, count in SQL)
1209
- #
1210
- # @example
1211
- # session.zrangebyscore('myzset', '-inf', '+inf')
1212
- # # => ['one', 'two', 'three']
1213
- #
1214
- # @example
1215
- # session.zrangebyscore('myzset', '(1', 2, withscores: true)
1216
- # # => [['two', '2']]
1217
- #
1218
- # @example
1219
- # opts = { withscores: true, limit: [1, 1] }
1220
- # session.zrangebyscore('myzset', '-inf', '+inf', opts)
1221
- # # => [['two', '2']]
1222
- #
1223
- # @return [Array] list of elements in the specified score range (optionally
1224
- # with their scores, in case the :withscores option is given)
1225
- def zrangebyscore(key, min, max, opts = {})
1226
- args = [:ZRANGEBYSCORE, key, min, max]
1227
- args << :WITHSCORES if opts[:withscores]
1228
- args.push(:LIMIT).concat(opts[:limit]) if opts[:limit].is_a?(Array)
1229
-
1230
- run(*args)
1231
- end
1232
-
1233
- # Determine the index of a member in a sorted set
1234
- # @see http://redis.io/commands/zrank
1235
- #
1236
- # @param [String] key
1237
- # @param [String] member
1238
- #
1239
- # @return [Integer, nil] the rank of member or nil if member does not exist
1240
- # in the sorted set or key does not exist
1241
- def zrank(key, member)
1242
- run(:ZRANK, key, member)
1243
- end
1244
-
1245
- # Remove one or more members from a sorted set
1246
- # @see http://redis.io/commands/zrem
1247
- #
1248
- # @param [String] key
1249
- # @param [Array<String>] members to delete
1250
- #
1251
- # @return [Integer] number of deleted members
1252
- # @return [RError] when key exists and does not hold a sorted set.
1253
- def zrem(key, *members)
1254
- run(*members.unshift(:ZREM, key))
1255
- end
1256
-
1257
- # Remove all members in a sorted set within the given indexes
1258
- # @see http://redis.io/commands/zremrangebyrank
1259
- #
1260
- # @param [String] key
1261
- # @param [String] start
1262
- # @param [String] stop
1263
- #
1264
- # @return [Integer] the number of elements removed
1265
- def zremrangebyrank(key, start, stop)
1266
- run(:ZREMRANGEBYRANK, key, start, stop)
1267
- end
1268
-
1269
- # Remove all members in a sorted set within the given scores
1270
- # @see http://redis.io/commands/zremrangebyscore
1271
- #
1272
- # @param [String] key
1273
- # @param [String] min score
1274
- # @param [String] max score
1275
- #
1276
- # @return [Integer] the number of elements removed
1277
- def zremrangebyscore(key, min, max)
1278
- run(:ZREMRANGEBYSCORE, key, min, max)
1279
- end
1280
-
1281
- # Return a range of members in a sorted set, by index, with scores ordered
1282
- # from high to low
1283
- # @see http://redis.io/commands/zrevrange
1284
- #
1285
- # @example
1286
- # session.zrevrange('myzset', 0, -1)
1287
- # # => ['two', 'one']
1288
- #
1289
- # @example
1290
- # session.zrevrange('myzset', 0, -1, withscores: true)
1291
- # # => [['two', '2'], ['one', '1']]
1292
- #
1293
- # @param [String] key
1294
- # @param [Integer] start index
1295
- # @param [Integer] stop index
1296
- # @param [Hash] opts
1297
- #
1298
- # @option opts [Boolean] :withscores (false) Return the scores of
1299
- # the elements together with the elements
1300
- #
1301
- # @return [Array] list of elements in the specified range (optionally with
1302
- # their scores, in case the :withscores option is given)
1303
- def zrevrange(key, start, stop, opts = {})
1304
- args = [:ZREVRANGE, key, start, stop]
1305
- args << :WITHSCORES if opts[:withscores]
1306
- run(*args)
1307
- end
1308
-
1309
- # Return a range of members in a sorted set, by score, with scores ordered
1310
- # from high to low
1311
- # @see http://redis.io/commands/zrevrangebyscore
1312
- #
1313
- # @param [String] key under which set is stored
1314
- # @param [String] min score
1315
- # @param [String] max score
1316
- # @param [Hash] opts
1317
- #
1318
- # @option opts [Boolean] :withscores (false) Return the scores of
1319
- # the elements together with the elements
1320
- # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
1321
- # elements (similar to SELECT LIMIT offset, count in SQL)
1322
- #
1323
- # @example
1324
- # session.zrevrangebyscore('myzset', '+inf', '-inf')
1325
- # # => ['three', 'two', 'one']
1326
- #
1327
- # @example
1328
- # session.zrevrangebyscore('myzset', 2, '(1', withscores: true)
1329
- # # => [['two', '2']]
1330
- #
1331
- # @example
1332
- # opts = { withscores: true, limit: [1, 1] }
1333
- # session.zrevrangebyscore('myzset', '+inf', '-inf', opts)
1334
- # # => [['two', '2']]
1335
- #
1336
- # @return [Array] list of elements in the specified score range (optionally
1337
- # with their scores, in case the :withscores option is given)
1338
- def zrevrangebyscore(key, min, max, opts = {})
1339
- args = [:ZREVRANGEBYSCORE, key, min, max]
1340
- args << :WITHSCORES if opts[:withscores]
1341
- args.push(:LIMIT).concat(opts[:limit]) if opts[:limit].is_a?(Array)
1342
-
1343
- run(*args)
1344
- end
1345
-
1346
- # Determine the index of a member in a sorted set, with scores ordered from
1347
- # high to low
1348
- # @see http://redis.io/commands/zrevrank
1349
- #
1350
- # @param [String] key
1351
- # @param [String] member
1352
- #
1353
- # @return [Integer, nil] the rank of member, or nil if member does not
1354
- # exists in the sorted set or key does not exists
1355
- def zrevrank(key, member)
1356
- run(:ZREVRANK, key, member)
1357
- end
1358
-
1359
- # Get the score associated with the given member in a sorted set
1360
- # @see http://redis.io/commands/zscore
1361
- #
1362
- # @param [String] key
1363
- # @param [String] member
1364
- #
1365
- # @return [String, nil] the score of member (a double precision floating
1366
- # point number), represented as string, or nil if member does not exist in
1367
- # the sorted set, or key does not exists
1368
- def zscore(key, member)
1369
- run(:ZSCORE, key, member)
1370
- end
1371
-
1372
- protected
1373
-
1374
- def serialize(*command)
1375
- Protocol.build_command(*command)
1376
- end
22
+ private
1377
23
 
1378
24
  def run(*command)
1379
- @connection.run_command(*command)
25
+ response = @connection.run_command(*command)
26
+ error?(response) ? (raise response) : response
1380
27
  end
1381
28
 
1382
- private
1383
-
1384
29
  def error?(response)
1385
30
  Protocol::RError === response
1386
31
  end