lib/memcache.rb in memcache-client-1.4.0 vs lib/memcache.rb in memcache-client-1.5.0

- old
+ new

@@ -40,11 +40,11 @@ class MemCache ## # The version of MemCache you are using. - VERSION = '1.4.0' + VERSION = '1.5.0' ## # Default options for the cache object. DEFAULT_OPTIONS = { @@ -364,10 +364,35 @@ ensure @mutex.unlock if @multithread end ## + # Flush the cache from all memcache servers. + + def flush_all + raise MemCacheError, 'No active servers' unless active? + raise MemCacheError, "Update of readonly cache" if @readonly + begin + @mutex.lock if @multithread + @servers.each do |server| + begin + sock = server.socket + raise MemCacheError, "No connection to server" if sock.nil? + sock.write "flush_all\r\n" + result = sock.gets + raise MemCacheError, $2.strip if result =~ /^(SERVER_)?ERROR(.*)/ + rescue SocketError, SystemCallError, IOError => err + server.close + raise MemCacheError, err.message + end + end + ensure + @mutex.unlock if @multithread + end + end + + ## # Reset the connection to all memcache servers. This should be called if # there is a problem with a cache lookup that might have left the connection # in a corrupted state. def reset @@ -382,30 +407,30 @@ # # Example: # # >> pp CACHE.stats # {"localhost:11211"=> - # {"bytes"=>"4718", - # "pid"=>"20188", - # "connection_structures"=>"4", - # "time"=>"1162278121", - # "pointer_size"=>"32", - # "limit_maxbytes"=>"67108864", - # "cmd_get"=>"14532", + # {"bytes"=>4718, + # "pid"=>20188, + # "connection_structures"=>4, + # "time"=>1162278121, + # "pointer_size"=>32, + # "limit_maxbytes"=>67108864, + # "cmd_get"=>14532, # "version"=>"1.2.0", - # "bytes_written"=>"432583", - # "cmd_set"=>"32", - # "get_misses"=>"0", - # "total_connections"=>"19", - # "curr_connections"=>"3", - # "curr_items"=>"4", - # "uptime"=>"1557", - # "get_hits"=>"14532", - # "total_items"=>"32", - # "rusage_system"=>"0.313952", - # "rusage_user"=>"0.119981", - # "bytes_read"=>"190619"}} + # "bytes_written"=>432583, + # "cmd_set"=>32, + # "get_misses"=>0, + # "total_connections"=>19, + # "curr_connections"=>3, + # "curr_items"=>4, + # "uptime"=>1557, + # "get_hits"=>14532, + # "total_items"=>32, + # "rusage_system"=>0.313952, + # "rusage_user"=>0.119981, + # "bytes_read"=>190619}} # => nil def stats raise MemCacheError, "No active servers" unless active? server_stats = {} @@ -416,15 +441,30 @@ value = nil begin sock.write "stats\r\n" stats = {} - while line = sock.gets + while line = sock.gets do break if line == "END\r\n" - line =~ /^STAT ([\w]+) ([\d.]+)/ - stats[$1] = $2 + if line =~ /^STAT ([\w]+) ([\w\.\:]+)/ then + name, value = $1, $2 + stats[name] = case name + when 'version' + value + when 'rusage_user', 'rusage_system' then + seconds, microseconds = value.split(/:/, 2) + microseconds ||= 0 + Float(seconds) + (Float(microseconds) / 1_000_000) + else + if value =~ /^\d+$/ then + value.to_i + else + value + end + end + end end - server_stats["#{server.host}:#{server.port}"] = stats.clone + server_stats["#{server.host}:#{server.port}"] = stats rescue SocketError, SystemCallError, IOError => err server.close raise MemCacheError, err.message end end