dalli 2.7.2 → 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,5 +1,7 @@
1
- require 'digest/sha1'
2
- require 'zlib'
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/sha1"
4
+ require "zlib"
3
5
 
4
6
  module Dalli
5
7
  class Ring
@@ -15,12 +17,12 @@ module Dalli
15
17
  continuum = []
16
18
  servers.each do |server|
17
19
  entry_count_for(server, servers.size, total_weight).times do |idx|
18
- hash = Digest::SHA1.hexdigest("#{server.hostname}:#{server.port}:#{idx}")
20
+ hash = Digest::SHA1.hexdigest("#{server.name}:#{idx}")
19
21
  value = Integer("0x#{hash[0..7]}")
20
22
  continuum << Dalli::Ring::Entry.new(value, server)
21
23
  end
22
24
  end
23
- @continuum = continuum.sort { |a, b| a.value <=> b.value }
25
+ @continuum = continuum.sort_by(&:value)
24
26
  end
25
27
 
26
28
  threadsafe! unless options[:threadsafe] == false
@@ -31,7 +33,13 @@ module Dalli
31
33
  if @continuum
32
34
  hkey = hash_for(key)
33
35
  20.times do |try|
34
- 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
35
43
  server = @continuum[entryidx].server
36
44
  return server if server.alive?
37
45
  break unless @failover
@@ -39,18 +47,18 @@ module Dalli
39
47
  end
40
48
  else
41
49
  server = @servers.first
42
- return server if server && server.alive?
50
+ return server if server&.alive?
43
51
  end
44
52
 
45
53
  raise Dalli::RingError, "No server available"
46
54
  end
47
55
 
48
56
  def lock
49
- @servers.each { |s| s.lock! }
57
+ @servers.each(&:lock!)
50
58
  begin
51
- return yield
59
+ yield
52
60
  ensure
53
- @servers.each { |s| s.unlock! }
61
+ @servers.each(&:unlock!)
54
62
  end
55
63
  end
56
64
 
@@ -70,64 +78,6 @@ module Dalli
70
78
  ((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
71
79
  end
72
80
 
73
- # Native extension to perform the binary search within the continuum
74
- # space. Fallback to a pure Ruby version if the compilation doesn't work.
75
- # optional for performance and only necessary if you are using multiple
76
- # memcached servers.
77
- begin
78
- require 'inline'
79
- inline do |builder|
80
- builder.c <<-EOM
81
- int binary_search(VALUE ary, unsigned int r) {
82
- long upper = RARRAY_LEN(ary) - 1;
83
- long lower = 0;
84
- long idx = 0;
85
- ID value = rb_intern("value");
86
- VALUE continuumValue;
87
- unsigned int l;
88
-
89
- while (lower <= upper) {
90
- idx = (lower + upper) / 2;
91
-
92
- continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0);
93
- l = NUM2UINT(continuumValue);
94
- if (l == r) {
95
- return idx;
96
- }
97
- else if (l > r) {
98
- upper = idx - 1;
99
- }
100
- else {
101
- lower = idx + 1;
102
- }
103
- }
104
- return upper;
105
- }
106
- EOM
107
- end
108
- rescue LoadError
109
- # Find the closest index in the Ring with value <= the given value
110
- def binary_search(ary, value)
111
- upper = ary.size - 1
112
- lower = 0
113
- idx = 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
- return 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