Class: MemCacheWithConsistentHashing

Inherits:
MemCache
  • Object
show all
Includes:
ConsistentHashing
Defined in:
lib/mem_cache_with_consistent_hashing.rb

Instance Method Summary collapse

Instance Method Details

#get_server_for_key(key) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mem_cache_with_consistent_hashing.rb', line 50

def get_server_for_key(key)
  raise ArgumentError, "illegal character in key #{key.inspect}" if
    key =~ /\s/
  raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
  raise MemCacheError, "No servers available" if @servers.empty?
  return @servers.first if @servers.length == 1

  hkey = hash_for key

  20.times do |try|
    server = @continuum.find_server(hkey)
    return server if server.alive?
    hkey += hash_for "#{try}#{key}"
  end

  raise MemCache::MemCacheError, "No servers available"
end

#hash_for(key) ⇒ Object



46
47
48
# File 'lib/mem_cache_with_consistent_hashing.rb', line 46

def hash_for(key)
  @continuum.hash_to_unit_circle(key)
end

#servers=(servers) ⇒ Object



42
43
44
# File 'lib/mem_cache_with_consistent_hashing.rb', line 42

def servers=(servers)
  @continuum = Continuum.new(super)
end