1
$TESTING = defined?($TESTING) && $TESTING
10
# A Ruby client library for memcached.
16
# The version of MemCache you are using.
21
# Default options for the cache object.
34
# Default memcached port.
39
# Default memcached server weight.
44
# The namespace for this instance
46
attr_reader :namespace
49
# The multithread setting for this instance
51
attr_reader :multithread
54
# The servers this client talks to. Play at your own peril.
59
# Socket timeout limit with this client, defaults to 0.5 sec.
60
# Set to nil to disable timeouts.
65
# Should the client try to failover to another server if the
66
# first server is down? Defaults to true.
71
# Log debug/info/warn/error to the given Logger, defaults to nil.
76
# Don't send or look for a reply from the memcached server for write operations.
77
# Please note this feature only works in memcached 1.2.5 and later. Earlier
78
# versions will reply with "ERROR".
82
# Accepts a list of +servers+ and a list of +opts+. +servers+ may be
83
# omitted. See +servers=+ for acceptable server list arguments.
85
# Valid options for +opts+ are:
87
# [:namespace] Prepends this value to all keys added or retrieved.
88
# [:readonly] Raises an exception on cache writes when true.
89
# [:multithread] Wraps cache access in a Mutex for thread safety. Defaults to true.
90
# [:failover] Should the client try to failover to another server if the
91
# first server is down? Defaults to true.
92
# [:timeout] Time to use as the socket read timeout. Defaults to 0.5 sec,
93
# set to nil to disable timeouts (this is a major performance penalty in Ruby 1.8,
94
# "gem install SystemTimer' to remove most of the penalty).
95
# [:logger] Logger to use for info/debug output, defaults to nil
96
# [:no_reply] Don't bother looking for a reply for write operations (i.e. they
97
# become 'fire and forget'), memcached 1.2.5 and later only, speeds up
98
# set/add/delete/incr/decr significantly.
100
# Other options are ignored.
102
def initialize(*args)
111
when Hash then opts = arg
112
when Array then servers = arg
113
when String then servers = [arg]
114
else raise ArgumentError, 'first argument must be Array, Hash or String'
119
raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
122
opts = DEFAULT_OPTIONS.merge opts
123
@namespace = opts[:namespace]
124
@readonly = opts[:readonly]
125
@multithread = opts[:multithread]
126
@timeout = opts[:timeout]
127
@failover = opts[:failover]
128
@logger = opts[:logger]
129
@no_reply = opts[:no_reply]
130
@mutex = Mutex.new if @multithread
132
logger.info { "memcache-client #{VERSION} #{Array(servers).inspect}" } if logger
134
Thread.current[:memcache_client] = self.object_id if !@multithread
136
self.servers = servers
140
# Returns a string representation of the cache object.
143
"<MemCache: %d servers, ns: %p, ro: %p>" %
144
[@servers.length, @namespace, @readonly]
148
# Returns whether there is at least one active server for the object.
155
# Returns whether or not the cache object was created read only.
162
# Set the servers that the requests will be distributed between. Entries
163
# can be either strings of the form "hostname:port" or
164
# "hostname:port:weight" or MemCache::Server objects.
166
def servers=(servers)
167
# Create the server objects.
168
@servers = Array(servers).collect do |server|
171
host, port, weight = server.split ':', 3
172
port ||= DEFAULT_PORT
173
weight ||= DEFAULT_WEIGHT
174
Server.new self, host, port, weight
180
logger.debug { "Servers now: #{@servers.inspect}" } if logger
182
# There's no point in doing this if there's only one server
183
@continuum = create_continuum_for(@servers) if @servers.size > 1
189
# Decrements the value for +key+ by +amount+ and returns the new value.
190
# +key+ must already exist. If +key+ is not an integer, it is assumed to be
191
# 0. +key+ can not be decremented below 0.
193
def decr(key, amount = 1)
194
raise MemCacheError, "Update of readonly cache" if @readonly
195
with_server(key) do |server, cache_key|
196
cache_decr server, cache_key, amount
198
rescue TypeError => err
199
handle_error nil, err
203
# Retrieves +key+ from memcache. If +raw+ is false, the value will be
206
def get(key, raw = false)
207
with_server(key) do |server, cache_key|
208
logger.debug { "get #{key} from #{server.inspect}" } if logger
209
value = cache_get server, cache_key
210
return nil if value.nil?
211
value = Marshal.load value unless raw
214
rescue TypeError => err
215
handle_error nil, err
219
# Performs a +get+ with the given +key+. If
220
# the value does not exist and a block was given,
221
# the block will be called and the result saved via +add+.
223
# If you do not provide a block, using this
224
# method is the same as using +get+.
226
def fetch(key, expiry = 0, raw = false)
227
value = get(key, raw)
229
if value.nil? && block_given?
231
add(key, value, expiry, raw)
238
# Retrieves multiple values from memcached in parallel, if possible.
240
# The memcached protocol supports the ability to retrieve multiple
241
# keys in a single request. Pass in an array of keys to this method
244
# 1. map the key to the appropriate memcached server
245
# 2. send a single request to each server that has one or more key values
247
# Returns a hash of values.
251
# cache.get_multi "a", "b" # => { "a" => 1, "b" => 2 }
253
# Note that get_multi assumes the values are marshalled.
256
raise MemCacheError, 'No active servers' unless active?
259
key_count = keys.length
261
server_keys = Hash.new { |h,k| h[k] = [] }
263
# map keys to servers
265
server, cache_key = request_setup key
266
cache_keys[cache_key] = key
267
server_keys[server] << cache_key
272
server_keys.each do |server, keys_for_server|
273
keys_for_server_str = keys_for_server.join ' '
275
values = cache_get_multi server, keys_for_server_str
276
values.each do |key, value|
277
results[cache_keys[key]] = Marshal.load value
279
rescue IndexError => e
280
# Ignore this server and try the others
281
logger.warn { "Unable to retrieve #{keys_for_server.size} elements from #{server.inspect}: #{e.message}"} if logger
286
rescue TypeError => err
287
handle_error nil, err
291
# Increments the value for +key+ by +amount+ and returns the new value.
292
# +key+ must already exist. If +key+ is not an integer, it is assumed to be
295
def incr(key, amount = 1)
296
raise MemCacheError, "Update of readonly cache" if @readonly
297
with_server(key) do |server, cache_key|
298
cache_incr server, cache_key, amount
300
rescue TypeError => err
301
handle_error nil, err
305
# Add +key+ to the cache with value +value+ that expires in +expiry+
306
# seconds. If +raw+ is true, +value+ will not be Marshalled.
308
# Warning: Readers should not call this method in the event of a cache miss;
313
def set(key, value, expiry = 0, raw = false)
314
raise MemCacheError, "Update of readonly cache" if @readonly
315
with_server(key) do |server, cache_key|
317
value = Marshal.dump value unless raw
318
logger.debug { "set #{key} to #{server.inspect}: #{value.to_s.size}" } if logger
320
raise MemCacheError, "Value too large, memcached can only store 1MB of data per key" if value.to_s.size > ONE_MB
322
command = "set #{cache_key} 0 #{expiry} #{value.to_s.size}#{noreply}\r\n#{value}\r\n"
324
with_socket_management(server) do |socket|
326
break nil if @no_reply
328
raise_on_error_response! result
332
raise MemCacheError, "lost connection to #{server.host}:#{server.port}"
341
# "cas" is a check and set operation which means "store this data but
342
# only if no one else has updated since I last fetched it." This can
343
# be used as a form of optimistic locking.
345
# Works in block form like so:
346
# cache.cas('some-key') do |value|
351
# +nil+ if the value was not found on the memcached server.
352
# +STORED+ if the value was updated successfully
353
# +EXISTS+ if the value was updated by someone else since last fetch
355
def cas(key, expiry=0, raw=false)
356
raise MemCacheError, "Update of readonly cache" if @readonly
357
raise MemCacheError, "A block is required" unless block_given?
359
(value, token) = gets(key, raw)
360
return nil unless value
361
updated = yield value
363
with_server(key) do |server, cache_key|
365
value = Marshal.dump updated unless raw
366
logger.debug { "cas #{key} to #{server.inspect}: #{value.to_s.size}" } if logger
367
command = "cas #{cache_key} 0 #{expiry} #{value.to_s.size} #{token}#{noreply}\r\n#{value}\r\n"
369
with_socket_management(server) do |socket|
371
break nil if @no_reply
373
raise_on_error_response! result
377
raise MemCacheError, "lost connection to #{server.host}:#{server.port}"
386
# Add +key+ to the cache with value +value+ that expires in +expiry+
387
# seconds, but only if +key+ does not already exist in the cache.
388
# If +raw+ is true, +value+ will not be Marshalled.
390
# Readers should call this method in the event of a cache miss, not
393
def add(key, value, expiry = 0, raw = false)
394
raise MemCacheError, "Update of readonly cache" if @readonly
395
with_server(key) do |server, cache_key|
396
value = Marshal.dump value unless raw
397
logger.debug { "add #{key} to #{server}: #{value ? value.to_s.size : 'nil'}" } if logger
398
command = "add #{cache_key} 0 #{expiry} #{value.to_s.size}#{noreply}\r\n#{value}\r\n"
400
with_socket_management(server) do |socket|
402
break nil if @no_reply
404
raise_on_error_response! result
411
# Add +key+ to the cache with value +value+ that expires in +expiry+
412
# seconds, but only if +key+ already exists in the cache.
413
# If +raw+ is true, +value+ will not be Marshalled.
414
def replace(key, value, expiry = 0, raw = false)
415
raise MemCacheError, "Update of readonly cache" if @readonly
416
with_server(key) do |server, cache_key|
417
value = Marshal.dump value unless raw
418
logger.debug { "replace #{key} to #{server}: #{value ? value.to_s.size : 'nil'}" } if logger
419
command = "replace #{cache_key} 0 #{expiry} #{value.to_s.size}#{noreply}\r\n#{value}\r\n"
421
with_socket_management(server) do |socket|
423
break nil if @no_reply
425
raise_on_error_response! result
432
# Append - 'add this data to an existing key after existing data'
433
# Please note the value is always passed to memcached as raw since it
434
# doesn't make a lot of sense to concatenate marshalled data together.
435
def append(key, value)
436
raise MemCacheError, "Update of readonly cache" if @readonly
437
with_server(key) do |server, cache_key|
438
logger.debug { "append #{key} to #{server}: #{value ? value.to_s.size : 'nil'}" } if logger
439
command = "append #{cache_key} 0 0 #{value.to_s.size}#{noreply}\r\n#{value}\r\n"
441
with_socket_management(server) do |socket|
443
break nil if @no_reply
445
raise_on_error_response! result
452
# Prepend - 'add this data to an existing key before existing data'
453
# Please note the value is always passed to memcached as raw since it
454
# doesn't make a lot of sense to concatenate marshalled data together.
455
def prepend(key, value)
456
raise MemCacheError, "Update of readonly cache" if @readonly
457
with_server(key) do |server, cache_key|
458
logger.debug { "prepend #{key} to #{server}: #{value ? value.to_s.size : 'nil'}" } if logger
459
command = "prepend #{cache_key} 0 0 #{value.to_s.size}#{noreply}\r\n#{value}\r\n"
461
with_socket_management(server) do |socket|
463
break nil if @no_reply
465
raise_on_error_response! result
472
# Removes +key+ from the cache in +expiry+ seconds.
474
def delete(key, expiry = 0)
475
raise MemCacheError, "Update of readonly cache" if @readonly
476
with_server(key) do |server, cache_key|
477
with_socket_management(server) do |socket|
478
logger.debug { "delete #{cache_key} on #{server}" } if logger
479
socket.write "delete #{cache_key} #{expiry}#{noreply}\r\n"
480
break nil if @no_reply
482
raise_on_error_response! result
489
# Flush the cache from all memcache servers.
490
# A non-zero value for +delay+ will ensure that the flush
491
# is propogated slowly through your memcached server farm.
492
# The Nth server will be flushed N*delay seconds from now,
493
# asynchronously so this method returns quickly.
494
# This prevents a huge database spike due to a total
497
def flush_all(delay=0)
498
raise MemCacheError, 'No active servers' unless active?
499
raise MemCacheError, "Update of readonly cache" if @readonly
503
@servers.each do |server|
504
with_socket_management(server) do |socket|
505
logger.debug { "flush_all #{delay_time} on #{server}" } if logger
506
if delay == 0 # older versions of memcached will fail silently otherwise
507
socket.write "flush_all#{noreply}\r\n"
509
socket.write "flush_all #{delay_time}#{noreply}\r\n"
511
break nil if @no_reply
513
raise_on_error_response! result
518
rescue IndexError => err
519
handle_error nil, err
524
# Reset the connection to all memcache servers. This should be called if
525
# there is a problem with a cache lookup that might have left the connection
526
# in a corrupted state.
529
@servers.each { |server| server.close }
533
# Returns statistics for each memcached server. An explanation of the
534
# statistics can be found in the memcached docs:
536
# http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
541
# {"localhost:11211"=>
544
# "connection_structures"=>4,
545
# "time"=>1162278121,
546
# "pointer_size"=>32,
547
# "limit_maxbytes"=>67108864,
549
# "version"=>"1.2.0",
550
# "bytes_written"=>432583,
553
# "total_connections"=>19,
554
# "curr_connections"=>3,
559
# "rusage_system"=>0.313952,
560
# "rusage_user"=>0.119981,
561
# "bytes_read"=>190619}}
565
raise MemCacheError, "No active servers" unless active?
568
@servers.each do |server|
569
next unless server.alive?
571
with_socket_management(server) do |socket|
573
socket.write "stats\r\n"
575
while line = socket.gets do
576
raise_on_error_response! line
577
break if line == "END\r\n"
578
if line =~ /\ASTAT ([\S]+) ([\w\.\:]+)/ then
580
stats[name] = case name
583
when 'rusage_user', 'rusage_system' then
584
seconds, microseconds = value.split(/:/, 2)
586
Float(seconds) + (Float(microseconds) / 1_000_000)
588
if value =~ /\A\d+\Z/ then
596
server_stats["#{server.host}:#{server.port}"] = stats
600
raise MemCacheError, "No active servers" if server_stats.empty?
605
# Shortcut to get a value from the cache.
610
# Shortcut to save a value in the cache. This method does not set an
611
# expiration on the entry. Use set to specify an explicit expiry.
617
protected unless $TESTING
620
# Create a key for the cache, incorporating the namespace qualifier if
623
def make_cache_key(key)
624
if namespace.nil? then
627
"#{@namespace}:#{key}"
632
# Returns an interoperable hash value for +key+. (I think, docs are
633
# sketchy for down servers).
640
# Pick a server to handle the request based on a hash of the key.
642
def get_server_for_key(key, options = {})
643
raise ArgumentError, "illegal character in key #{key.inspect}" if
645
raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
646
raise MemCacheError, "No servers available" if @servers.empty?
647
return @servers.first if @servers.length == 1
652
entryidx = Continuum.binary_search(@continuum, hkey)
653
server = @continuum[entryidx].server
654
return server if server.alive?
655
break unless failover
656
hkey = hash_for "#{try}#{key}"
659
raise MemCacheError, "No servers available"
663
# Performs a raw decr for +cache_key+ from +server+. Returns nil if not
666
def cache_decr(server, cache_key, amount)
667
with_socket_management(server) do |socket|
668
socket.write "decr #{cache_key} #{amount}#{noreply}\r\n"
669
break nil if @no_reply
671
raise_on_error_response! text
672
return nil if text == "NOT_FOUND\r\n"
678
# Fetches the raw data for +cache_key+ from +server+. Returns nil on cache
681
def cache_get(server, cache_key)
682
with_socket_management(server) do |socket|
683
socket.write "get #{cache_key}\r\n"
684
keyline = socket.gets # "VALUE <key> <flags> <bytes>\r\n"
688
raise MemCacheError, "lost connection to #{server.host}:#{server.port}"
691
raise_on_error_response! keyline
692
return nil if keyline == "END\r\n"
694
unless keyline =~ /(\d+)\r/ then
696
raise MemCacheError, "unexpected response #{keyline.inspect}"
698
value = socket.read $1.to_i
699
socket.read 2 # "\r\n"
700
socket.gets # "END\r\n"
705
def gets(key, raw = false)
706
with_server(key) do |server, cache_key|
707
logger.debug { "gets #{key} from #{server.inspect}" } if logger
708
result = with_socket_management(server) do |socket|
709
socket.write "gets #{cache_key}\r\n"
710
keyline = socket.gets # "VALUE <key> <flags> <bytes> <cas token>\r\n"
714
raise MemCacheError, "lost connection to #{server.host}:#{server.port}"
717
raise_on_error_response! keyline
718
return nil if keyline == "END\r\n"
720
unless keyline =~ /(\d+) (\w+)\r/ then
722
raise MemCacheError, "unexpected response #{keyline.inspect}"
724
value = socket.read $1.to_i
725
socket.read 2 # "\r\n"
726
socket.gets # "END\r\n"
729
result[0] = Marshal.load result[0] unless raw
732
rescue TypeError => err
733
handle_error nil, err
738
# Fetches +cache_keys+ from +server+ using a multi-get.
740
def cache_get_multi(server, cache_keys)
741
with_socket_management(server) do |socket|
743
socket.write "get #{cache_keys}\r\n"
745
while keyline = socket.gets do
746
return values if keyline == "END\r\n"
747
raise_on_error_response! keyline
749
unless keyline =~ /\AVALUE (.+) (.+) (.+)/ then
751
raise MemCacheError, "unexpected response #{keyline.inspect}"
754
key, data_length = $1, $3
755
values[$1] = socket.read data_length.to_i
756
socket.read(2) # "\r\n"
760
raise MemCacheError, "lost connection to #{server.host}:#{server.port}" # TODO: retry here too
765
# Performs a raw incr for +cache_key+ from +server+. Returns nil if not
768
def cache_incr(server, cache_key, amount)
769
with_socket_management(server) do |socket|
770
socket.write "incr #{cache_key} #{amount}#{noreply}\r\n"
771
break nil if @no_reply
773
raise_on_error_response! text
774
return nil if text == "NOT_FOUND\r\n"
780
# Gets or creates a socket connected to the given server, and yields it
781
# to the block, wrapped in a mutex synchronization if @multithread is true.
783
# If a socket error (SocketError, SystemCallError, IOError) or protocol error
784
# (MemCacheError) is raised by the block, closes the socket, attempts to
785
# connect again, and retries the block (once). If an error is again raised,
786
# reraises it as MemCacheError.
788
# If unable to connect to the server (or if in the reconnect wait period),
789
# raises MemCacheError. Note that the socket connect code marks a server
790
# dead for a timeout period, so retrying does not apply to connection attempt
791
# failures (but does still apply to unexpectedly lost connections etc.).
793
def with_socket_management(server, &block)
794
check_multithread_status!
796
@mutex.lock if @multithread
800
socket = server.socket
802
# Raise an IndexError to show this server is out of whack. If were inside
803
# a with_server block, we'll catch it and attempt to restart the operation.
805
raise IndexError, "No connection to server (#{server.status})" if socket.nil?
809
rescue SocketError, Errno::EAGAIN, Timeout::Error => err
810
logger.warn { "Socket failure: #{err.message}" } if logger
811
server.mark_dead(err)
812
handle_error(server, err)
814
rescue MemCacheError, SystemCallError, IOError => err
815
logger.warn { "Generic failure: #{err.class.name}: #{err.message}" } if logger
816
handle_error(server, err) if retried || socket.nil?
821
@mutex.unlock if @multithread
827
server, cache_key = request_setup(key)
828
yield server, cache_key
829
rescue IndexError => e
830
logger.warn { "Server failed: #{e.class.name}: #{e.message}" } if logger
831
if !retried && @servers.size > 1
832
logger.info { "Connection to server #{server.inspect} DIED! Retrying operation..." } if logger
841
# Handles +error+ from +server+.
843
def handle_error(server, error)
844
raise error if error.is_a?(MemCacheError)
845
server.close if server
846
new_error = MemCacheError.new error.message
847
new_error.set_backtrace error.backtrace
852
@no_reply ? ' noreply' : ''
856
# Performs setup for making a request with +key+ from memcached. Returns
857
# the server to fetch the key from and the complete key to use.
859
def request_setup(key)
860
raise MemCacheError, 'No active servers' unless active?
861
cache_key = make_cache_key key
862
server = get_server_for_key cache_key
863
return server, cache_key
866
def raise_on_error_response!(response)
867
if response =~ /\A(?:CLIENT_|SERVER_)?ERROR(.*)/
868
raise MemCacheError, $1.strip
872
def create_continuum_for(servers)
873
total_weight = servers.inject(0) { |memo, srv| memo + srv.weight }
876
servers.each do |server|
877
entry_count_for(server, servers.size, total_weight).times do |idx|
878
hash = Digest::SHA1.hexdigest("#{server.host}:#{server.port}:#{idx}")
879
value = Integer("0x#{hash[0..7]}")
880
continuum << Continuum::Entry.new(value, server)
884
continuum.sort { |a, b| a.value <=> b.value }
887
def entry_count_for(server, total_servers, total_weight)
888
((total_servers * Continuum::POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
891
def check_multithread_status!
892
return if @multithread
894
if Thread.current[:memcache_client] != self.object_id
895
raise MemCacheError, <<-EOM
896
You are accessing this memcache-client instance from multiple threads but have not enabled multithread support.
897
Normally: MemCache.new(['localhost:11211'], :multithread => true)
898
In Rails: config.cache_store = [:mem_cache_store, 'localhost:11211', { :multithread => true }]
904
# This class represents a memcached server instance.
909
# The amount of time to wait before attempting to re-establish a
910
# connection with a server that is marked dead.
915
# The host the memcached server is running on.
920
# The port the memcached server is listening on.
925
# The weight given to the server.
930
# The time of next retry if the connection is dead.
935
# A text status string describing the state of the server.
942
# Create a new MemCache::Server object for the memcached instance
943
# listening on the given host and port, weighted by the given weight.
945
def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT)
946
raise ArgumentError, "No host specified" if host.nil? or host.empty?
947
raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
951
@weight = weight.to_i
955
@status = 'NOT CONNECTED'
956
@timeout = memcache.timeout
957
@logger = memcache.logger
961
# Return a string representation of the server object.
964
"<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status]
968
# Check whether the server connection is alive. This will cause the
969
# socket to attempt to connect if it isn't already connected and or if
970
# the server was previously marked as down and the retry time has
978
# Try to connect to the memcached server targeted by this object.
979
# Returns the connected socket object on success or nil on failure.
982
return @sock if @sock and not @sock.closed?
986
# If the host was dead, don't retry for a while.
987
return if @retry and @retry > Time.now
989
# Attempt to connect if not already connected.
991
@sock = connect_to(@host, @port, @timeout)
992
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
994
@status = 'CONNECTED'
995
rescue SocketError, SystemCallError, IOError => err
996
logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
1003
def connect_to(host, port, timeout=nil)
1004
io = MemCache::BufferedIO.new(TCPSocket.new(host, port))
1005
io.read_timeout = timeout
1010
# Close the connection to the memcached server targeted by this
1011
# object. The server is not considered dead.
1014
@sock.close if @sock && !@sock.closed?
1017
@status = "NOT CONNECTED"
1021
# Mark the server as dead and close its socket.
1023
def mark_dead(error)
1024
@sock.close if @sock && !@sock.closed?
1026
@retry = Time.now + RETRY_DELAY
1028
reason = "#{error.class.name}: #{error.message}"
1029
@status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
1030
@logger.info { @status } if @logger
1036
# Base MemCache exception class.
1038
class MemCacheError < RuntimeError; end
1040
class BufferedIO < Net::BufferedIO # :nodoc:
1043
# An implementation similar to this is in *trunk* for 1.9. When it
1044
# gets released, this method can be removed when using 1.9
1047
@rbuf << @io.read_nonblock(BUFSIZE)
1048
rescue Errno::EWOULDBLOCK
1049
retry unless @read_timeout
1050
if IO.select([@io], nil, nil, @read_timeout)
1053
raise Timeout::Error, 'IO timeout'
1058
def setsockopt *args
1059
@io.setsockopt *args
1070
POINTS_PER_SERVER = 160 # this is the default in libmemcached
1072
# Find the closest index in Continuum with value <= the given value
1073
def self.binary_search(ary, value, &block)
1074
upper = ary.size - 1
1078
while(lower <= upper) do
1079
idx = (lower + upper) / 2
1080
comp = ary[idx].value <=> value
1097
def initialize(val, srv)
1103
"<#{value}, #{server.host}:#{server.port}>"