dalli 2.7.11 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dalli might be problematic. Click here for more details.

@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dalli
4
+ module Protocol
5
+ # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
6
+ class NilObject; end
7
+ NOT_FOUND = NilObject.new
8
+ end
9
+ end
data/lib/dalli/ring.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require 'digest/sha1'
3
- require 'zlib'
2
+
3
+ require "digest/sha1"
4
+ require "zlib"
4
5
 
5
6
  module Dalli
6
7
  class Ring
@@ -32,7 +33,13 @@ module Dalli
32
33
  if @continuum
33
34
  hkey = hash_for(key)
34
35
  20.times do |try|
35
- entryidx = binary_search(@continuum, hkey)
36
+ # Find the closest index in the Ring with value <= the given value
37
+ entryidx = @continuum.bsearch_index { |entry| entry.value > hkey }
38
+ if entryidx.nil?
39
+ entryidx = @continuum.size - 1
40
+ else
41
+ entryidx -= 1
42
+ end
36
43
  server = @continuum[entryidx].server
37
44
  return server if server.alive?
38
45
  break unless @failover
@@ -40,7 +47,7 @@ module Dalli
40
47
  end
41
48
  else
42
49
  server = @servers.first
43
- return server if server && server.alive?
50
+ return server if server&.alive?
44
51
  end
45
52
 
46
53
  raise Dalli::RingError, "No server available"
@@ -49,7 +56,7 @@ module Dalli
49
56
  def lock
50
57
  @servers.each(&:lock!)
51
58
  begin
52
- return yield
59
+ yield
53
60
  ensure
54
61
  @servers.each(&:unlock!)
55
62
  end
@@ -71,63 +78,6 @@ module Dalli
71
78
  ((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
72
79
  end
73
80
 
74
- # Native extension to perform the binary search within the continuum
75
- # space. Fallback to a pure Ruby version if the compilation doesn't work.
76
- # optional for performance and only necessary if you are using multiple
77
- # memcached servers.
78
- begin
79
- require 'inline'
80
- inline do |builder|
81
- builder.c <<-EOM
82
- int binary_search(VALUE ary, unsigned int r) {
83
- long upper = RARRAY_LEN(ary) - 1;
84
- long lower = 0;
85
- long idx = 0;
86
- ID value = rb_intern("value");
87
- VALUE continuumValue;
88
- unsigned int l;
89
-
90
- while (lower <= upper) {
91
- idx = (lower + upper) / 2;
92
-
93
- continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0);
94
- l = NUM2UINT(continuumValue);
95
- if (l == r) {
96
- return idx;
97
- }
98
- else if (l > r) {
99
- upper = idx - 1;
100
- }
101
- else {
102
- lower = idx + 1;
103
- }
104
- }
105
- return upper;
106
- }
107
- EOM
108
- end
109
- rescue LoadError
110
- # Find the closest index in the Ring with value <= the given value
111
- def binary_search(ary, value)
112
- upper = ary.size - 1
113
- lower = 0
114
-
115
- while (lower <= upper) do
116
- idx = (lower + upper) / 2
117
- comp = ary[idx].value <=> value
118
-
119
- if comp == 0
120
- return idx
121
- elsif comp > 0
122
- upper = idx - 1
123
- else
124
- lower = idx + 1
125
- end
126
- end
127
- upper
128
- end
129
- end
130
-
131
81
  class Entry
132
82
  attr_reader :value
133
83
  attr_reader :server
@@ -137,6 +87,5 @@ module Dalli
137
87
  @server = srv
138
88
  end
139
89
  end
140
-
141
90
  end
142
91
  end