# File lib/dalli/ring.rb, line 10 def initialize(servers, options) @servers = servers @continuum = nil if servers.size > 1 total_weight = servers.inject(0) { |memo, srv| memo + srv.weight } continuum = [] servers.each do |server| entry_count_for(server, servers.size, total_weight).times do |idx| hash = Digest::SHA1.hexdigest("#{server.hostname}:#{server.port}:#{idx}") value = Integer("0x#{hash[0..7]}") continuum << Dalli::Ring::Entry.new(value, server) end end @continuum = continuum.sort { |a, b| a.value <=> b.value } end threadsafe! unless options[:threadsafe] == false @failover = options[:failover] != false end
# File lib/dalli/ring.rb, line 48 def lock @servers.each { |s| s.lock! } begin return yield ensure @servers.each { |s| s.unlock! } end end
# File lib/dalli/ring.rb, line 30 def server_for_key(key) if @continuum hkey = hash_for(key) 20.times do |try| entryidx = binary_search(@continuum, hkey) server = @continuum[entryidx].server return server if server.alive? break unless @failover hkey = hash_for("#{try}#{key}") end else server = @servers.first return server if server && server.alive? end raise Dalli::RingError, "No server available" end
Generated with the Darkfish Rdoc Generator 2.