redis 4.1.3 → 4.2.3

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,9 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "command_helper"
2
4
  require_relative "registry"
3
5
  require_relative "../errors"
4
6
  require "em-synchrony"
5
7
  require "hiredis/reader"
6
8
 
9
+ Kernel.warn(
10
+ "The redis synchrony driver is deprecated and will be removed in redis-rb 5.0. " \
11
+ "We're looking for people to maintain it as a separate gem, see https://github.com/redis/redis-rb/issues/915"
12
+ )
13
+
7
14
  class Redis
8
15
  module Connection
9
16
  class RedisClient < EventMachine::Connection
@@ -46,9 +53,7 @@ class Redis
46
53
 
47
54
  def read
48
55
  @req = EventMachine::DefaultDeferrable.new
49
- if @timeout > 0
50
- @req.timeout(@timeout, :timeout)
51
- end
56
+ @req.timeout(@timeout, :timeout) if @timeout > 0
52
57
  EventMachine::Synchrony.sync @req
53
58
  end
54
59
 
@@ -105,7 +110,7 @@ class Redis
105
110
  end
106
111
 
107
112
  def connected?
108
- @connection && @connection.connected?
113
+ @connection&.connected?
109
114
  end
110
115
 
111
116
  def timeout=(timeout)
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "hash_ring"
2
4
 
3
5
  class Redis
4
6
  class Distributed
5
-
6
7
  class CannotDistribute < RuntimeError
7
8
  def initialize(command)
8
9
  @command = command
9
10
  end
10
11
 
11
12
  def message
12
- "#{@command.to_s.upcase} cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic."
13
+ "#{@command.to_s.upcase} cannot be used in Redis::Distributed because the keys involved need " \
14
+ "to be on the same server or because we cannot guarantee that the operation will be atomic."
13
15
  end
14
16
  end
15
17
 
@@ -22,10 +24,14 @@ class Redis
22
24
  @default_options = options.dup
23
25
  node_configs.each { |node_config| add_node(node_config) }
24
26
  @subscribed_node = nil
27
+ @watch_key = nil
25
28
  end
26
29
 
27
30
  def node_for(key)
28
- @ring.get_node(key_tag(key.to_s) || key.to_s)
31
+ key = key_tag(key.to_s) || key.to_s
32
+ raise CannotDistribute, :watch if @watch_key && @watch_key != key
33
+
34
+ @ring.get_node(key)
29
35
  end
30
36
 
31
37
  def nodes
@@ -33,9 +39,9 @@ class Redis
33
39
  end
34
40
 
35
41
  def add_node(options)
36
- options = { :url => options } if options.is_a?(String)
42
+ options = { url: options } if options.is_a?(String)
37
43
  options = @default_options.merge(options)
38
- @ring.add_node Redis.new( options )
44
+ @ring.add_node Redis.new(options)
39
45
  end
40
46
 
41
47
  # Change the selected database for the current connection.
@@ -144,12 +150,12 @@ class Redis
144
150
  end
145
151
 
146
152
  # Create a key using the serialized value, previously obtained using DUMP.
147
- def restore(key, ttl, serialized_value, options = {})
148
- node_for(key).restore(key, ttl, serialized_value, options)
153
+ def restore(key, ttl, serialized_value, **options)
154
+ node_for(key).restore(key, ttl, serialized_value, **options)
149
155
  end
150
156
 
151
157
  # Transfer a key from the connected instance to another instance.
152
- def migrate(key, options)
158
+ def migrate(_key, _options)
153
159
  raise CannotDistribute, :migrate
154
160
  end
155
161
 
@@ -170,8 +176,33 @@ class Redis
170
176
  end
171
177
 
172
178
  # Determine if a key exists.
173
- def exists(key)
174
- node_for(key).exists(key)
179
+ def exists(*args)
180
+ if !Redis.exists_returns_integer && args.size == 1
181
+ message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3, if you want to keep the old behavior, " \
182
+ "use `exists?` instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = true. " \
183
+ "(#{::Kernel.caller(1, 1).first})\n"
184
+
185
+ if defined?(::Warning)
186
+ ::Warning.warn(message)
187
+ else
188
+ warn(message)
189
+ end
190
+ exists?(*args)
191
+ else
192
+ keys_per_node = args.group_by { |key| node_for(key) }
193
+ keys_per_node.inject(0) do |sum, (node, keys)|
194
+ sum + node._exists(*keys)
195
+ end
196
+ end
197
+ end
198
+
199
+ # Determine if any of the keys exists.
200
+ def exists?(*args)
201
+ keys_per_node = args.group_by { |key| node_for(key) }
202
+ keys_per_node.each do |node, keys|
203
+ return true if node.exists?(*keys)
204
+ end
205
+ false
175
206
  end
176
207
 
177
208
  # Find all keys matching the given pattern.
@@ -204,11 +235,11 @@ class Redis
204
235
  end
205
236
 
206
237
  # Sort the elements in a list, set or sorted set.
207
- def sort(key, options = {})
238
+ def sort(key, **options)
208
239
  keys = [key, options[:by], options[:store], *Array(options[:get])].compact
209
240
 
210
241
  ensure_same_node(:sort, keys) do |node|
211
- node.sort(key, options)
242
+ node.sort(key, **options)
212
243
  end
213
244
  end
214
245
 
@@ -243,8 +274,8 @@ class Redis
243
274
  end
244
275
 
245
276
  # Set the string value of a key.
246
- def set(key, value, options = {})
247
- node_for(key).set(key, value, options)
277
+ def set(key, value, **options)
278
+ node_for(key).set(key, value, **options)
248
279
  end
249
280
 
250
281
  # Set the time to live in seconds of a key.
@@ -263,20 +294,20 @@ class Redis
263
294
  end
264
295
 
265
296
  # Set multiple keys to multiple values.
266
- def mset(*args)
297
+ def mset(*_args)
267
298
  raise CannotDistribute, :mset
268
299
  end
269
300
 
270
- def mapped_mset(hash)
301
+ def mapped_mset(_hash)
271
302
  raise CannotDistribute, :mapped_mset
272
303
  end
273
304
 
274
305
  # Set multiple keys to multiple values, only if none of the keys exist.
275
- def msetnx(*args)
306
+ def msetnx(*_args)
276
307
  raise CannotDistribute, :msetnx
277
308
  end
278
309
 
279
- def mapped_msetnx(hash)
310
+ def mapped_msetnx(_hash)
280
311
  raise CannotDistribute, :mapped_msetnx
281
312
  end
282
313
 
@@ -335,7 +366,7 @@ class Redis
335
366
  end
336
367
 
337
368
  # Return the position of the first bit set to 1 or 0 in a string.
338
- def bitpos(key, bit, start=nil, stop=nil)
369
+ def bitpos(key, bit, start = nil, stop = nil)
339
370
  node_for(key).bitpos(key, bit, start, stop)
340
371
  end
341
372
 
@@ -353,7 +384,7 @@ class Redis
353
384
  get(key)
354
385
  end
355
386
 
356
- def []=(key,value)
387
+ def []=(key, value)
357
388
  set(key, value)
358
389
  end
359
390
 
@@ -438,15 +469,9 @@ class Redis
438
469
 
439
470
  # Pop a value from a list, push it to another list and return it; or block
440
471
  # until one is available.
441
- def brpoplpush(source, destination, options = {})
442
- case options
443
- when Integer
444
- # Issue deprecation notice in obnoxious mode...
445
- options = { :timeout => options }
446
- end
447
-
472
+ def brpoplpush(source, destination, deprecated_timeout = 0, **options)
448
473
  ensure_same_node(:brpoplpush, [source, destination]) do |node|
449
- node.brpoplpush(source, destination, options)
474
+ node.brpoplpush(source, destination, deprecated_timeout, **options)
450
475
  end
451
476
  end
452
477
 
@@ -523,13 +548,13 @@ class Redis
523
548
  end
524
549
 
525
550
  # Scan a set
526
- def sscan(key, cursor, options={})
527
- node_for(key).sscan(key, cursor, options)
551
+ def sscan(key, cursor, **options)
552
+ node_for(key).sscan(key, cursor, **options)
528
553
  end
529
554
 
530
555
  # Scan a set and return an enumerator
531
- def sscan_each(key, options={}, &block)
532
- node_for(key).sscan_each(key, options, &block)
556
+ def sscan_each(key, **options, &block)
557
+ node_for(key).sscan_each(key, **options, &block)
533
558
  end
534
559
 
535
560
  # Subtract multiple sets.
@@ -584,6 +609,7 @@ class Redis
584
609
  def zadd(key, *args)
585
610
  node_for(key).zadd(key, *args)
586
611
  end
612
+ ruby2_keywords(:zadd) if respond_to?(:ruby2_keywords, true)
587
613
 
588
614
  # Increment the score of a member in a sorted set.
589
615
  def zincrby(key, increment, member)
@@ -601,14 +627,14 @@ class Redis
601
627
  end
602
628
 
603
629
  # Return a range of members in a sorted set, by index.
604
- def zrange(key, start, stop, options = {})
605
- node_for(key).zrange(key, start, stop, options)
630
+ def zrange(key, start, stop, **options)
631
+ node_for(key).zrange(key, start, stop, **options)
606
632
  end
607
633
 
608
634
  # Return a range of members in a sorted set, by index, with scores ordered
609
635
  # from high to low.
610
- def zrevrange(key, start, stop, options = {})
611
- node_for(key).zrevrange(key, start, stop, options)
636
+ def zrevrange(key, start, stop, **options)
637
+ node_for(key).zrevrange(key, start, stop, **options)
612
638
  end
613
639
 
614
640
  # Determine the index of a member in a sorted set.
@@ -628,14 +654,14 @@ class Redis
628
654
  end
629
655
 
630
656
  # Return a range of members in a sorted set, by score.
631
- def zrangebyscore(key, min, max, options = {})
632
- node_for(key).zrangebyscore(key, min, max, options)
657
+ def zrangebyscore(key, min, max, **options)
658
+ node_for(key).zrangebyscore(key, min, max, **options)
633
659
  end
634
660
 
635
661
  # Return a range of members in a sorted set, by score, with scores ordered
636
662
  # from high to low.
637
- def zrevrangebyscore(key, max, min, options = {})
638
- node_for(key).zrevrangebyscore(key, max, min, options)
663
+ def zrevrangebyscore(key, max, min, **options)
664
+ node_for(key).zrevrangebyscore(key, max, min, **options)
639
665
  end
640
666
 
641
667
  # Remove all members in a sorted set within the given scores.
@@ -650,16 +676,16 @@ class Redis
650
676
 
651
677
  # Intersect multiple sorted sets and store the resulting sorted set in a new
652
678
  # key.
653
- def zinterstore(destination, keys, options = {})
679
+ def zinterstore(destination, keys, **options)
654
680
  ensure_same_node(:zinterstore, [destination] + keys) do |node|
655
- node.zinterstore(destination, keys, options)
681
+ node.zinterstore(destination, keys, **options)
656
682
  end
657
683
  end
658
684
 
659
685
  # Add multiple sorted sets and store the resulting sorted set in a new key.
660
- def zunionstore(destination, keys, options = {})
686
+ def zunionstore(destination, keys, **options)
661
687
  ensure_same_node(:zunionstore, [destination] + keys) do |node|
662
- node.zunionstore(destination, keys, options)
688
+ node.zunionstore(destination, keys, **options)
663
689
  end
664
690
  end
665
691
 
@@ -668,9 +694,9 @@ class Redis
668
694
  node_for(key).hlen(key)
669
695
  end
670
696
 
671
- # Set the string value of a hash field.
672
- def hset(key, field, value)
673
- node_for(key).hset(key, field, value)
697
+ # Set multiple hash fields to multiple values.
698
+ def hset(key, *attrs)
699
+ node_for(key).hset(key, *attrs)
674
700
  end
675
701
 
676
702
  # Set the value of a hash field, only if the field does not exist.
@@ -742,7 +768,7 @@ class Redis
742
768
  end
743
769
 
744
770
  def subscribed?
745
- !! @subscribed_node
771
+ !!@subscribed_node
746
772
  end
747
773
 
748
774
  # Listen for messages published to the given channels.
@@ -760,7 +786,8 @@ class Redis
760
786
 
761
787
  # Stop listening for messages posted to the given channels.
762
788
  def unsubscribe(*channels)
763
- raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed?
789
+ raise "Can't unsubscribe if not subscribed." unless subscribed?
790
+
764
791
  @subscribed_node.unsubscribe(*channels)
765
792
  end
766
793
 
@@ -776,13 +803,26 @@ class Redis
776
803
  end
777
804
 
778
805
  # Watch the given keys to determine execution of the MULTI/EXEC block.
779
- def watch(*keys)
780
- raise CannotDistribute, :watch
806
+ def watch(*keys, &block)
807
+ ensure_same_node(:watch, keys) do |node|
808
+ @watch_key = key_tag(keys.first) || keys.first.to_s
809
+
810
+ begin
811
+ node.watch(*keys, &block)
812
+ rescue StandardError
813
+ @watch_key = nil
814
+ raise
815
+ end
816
+ end
781
817
  end
782
818
 
783
819
  # Forget about all watched keys.
784
820
  def unwatch
785
- raise CannotDistribute, :unwatch
821
+ raise CannotDistribute, :unwatch unless @watch_key
822
+
823
+ result = node_for(@watch_key).unwatch
824
+ @watch_key = nil
825
+ result
786
826
  end
787
827
 
788
828
  def pipelined
@@ -790,18 +830,30 @@ class Redis
790
830
  end
791
831
 
792
832
  # Mark the start of a transaction block.
793
- def multi
794
- raise CannotDistribute, :multi
833
+ def multi(&block)
834
+ raise CannotDistribute, :multi unless @watch_key
835
+
836
+ result = node_for(@watch_key).multi(&block)
837
+ @watch_key = nil if block_given?
838
+ result
795
839
  end
796
840
 
797
841
  # Execute all commands issued after MULTI.
798
842
  def exec
799
- raise CannotDistribute, :exec
843
+ raise CannotDistribute, :exec unless @watch_key
844
+
845
+ result = node_for(@watch_key).exec
846
+ @watch_key = nil
847
+ result
800
848
  end
801
849
 
802
850
  # Discard all commands issued after MULTI.
803
851
  def discard
804
- raise CannotDistribute, :discard
852
+ raise CannotDistribute, :discard unless @watch_key
853
+
854
+ result = node_for(@watch_key).discard
855
+ @watch_key = nil
856
+ result
805
857
  end
806
858
 
807
859
  # Control remote script registry.
@@ -860,7 +912,7 @@ class Redis
860
912
  self.class.new(@node_configs, @default_options)
861
913
  end
862
914
 
863
- protected
915
+ protected
864
916
 
865
917
  def on_each_node(command, *args)
866
918
  nodes.map do |node|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Redis
2
4
  # Base error for all redis-rb errors.
3
5
  class BaseError < RuntimeError
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zlib'
2
4
 
3
5
  class Redis
4
6
  class HashRing
5
-
6
7
  POINTS_PER_SERVER = 160 # this is the default in libmemcached
7
8
 
8
9
  attr_reader :ring, :sorted_keys, :replicas, :nodes
@@ -10,7 +11,7 @@ class Redis
10
11
  # nodes is a list of objects that have a proper to_s representation.
11
12
  # replicas indicates how many virtual points should be used pr. node,
12
13
  # replicas are required to improve the distribution.
13
- def initialize(nodes=[], replicas=POINTS_PER_SERVER)
14
+ def initialize(nodes = [], replicas = POINTS_PER_SERVER)
14
15
  @replicas = replicas
15
16
  @ring = {}
16
17
  @nodes = []
@@ -32,11 +33,11 @@ class Redis
32
33
  end
33
34
 
34
35
  def remove_node(node)
35
- @nodes.reject!{|n| n.id == node.id}
36
+ @nodes.reject! { |n| n.id == node.id }
36
37
  @replicas.times do |i|
37
38
  key = Zlib.crc32("#{node.id}:#{i}")
38
39
  @ring.delete(key)
39
- @sorted_keys.reject! {|k| k == key}
40
+ @sorted_keys.reject! { |k| k == key }
40
41
  end
41
42
  end
42
43
 
@@ -46,27 +47,29 @@ class Redis
46
47
  end
47
48
 
48
49
  def get_node_pos(key)
49
- return [nil,nil] if @ring.size == 0
50
+ return [nil, nil] if @ring.empty?
51
+
50
52
  crc = Zlib.crc32(key)
51
53
  idx = HashRing.binary_search(@sorted_keys, crc)
52
- return [@ring[@sorted_keys[idx]], idx]
54
+ [@ring[@sorted_keys[idx]], idx]
53
55
  end
54
56
 
55
57
  def iter_nodes(key)
56
- return [nil,nil] if @ring.size == 0
58
+ return [nil, nil] if @ring.empty?
59
+
57
60
  _, pos = get_node_pos(key)
58
61
  @ring.size.times do |n|
59
- yield @ring[@sorted_keys[(pos+n) % @ring.size]]
62
+ yield @ring[@sorted_keys[(pos + n) % @ring.size]]
60
63
  end
61
64
  end
62
65
 
63
66
  # Find the closest index in HashRing with value <= the given value
64
- def self.binary_search(ary, value, &block)
67
+ def self.binary_search(ary, value)
65
68
  upper = ary.size - 1
66
69
  lower = 0
67
70
  idx = 0
68
71
 
69
- while(lower <= upper) do
72
+ while lower <= upper
70
73
  idx = (lower + upper) / 2
71
74
  comp = ary[idx] <=> value
72
75
 
@@ -79,10 +82,8 @@ class Redis
79
82
  end
80
83
  end
81
84
 
82
- if upper < 0
83
- upper = ary.size - 1
84
- end
85
- return upper
85
+ upper = ary.size - 1 if upper < 0
86
+ upper
86
87
  end
87
88
  end
88
89
  end