Class: MemCache::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/memcache_server.rb

Overview

Extends the MemCache::Server object so that is is able to request stats.

Instance Method Summary collapse

Instance Method Details

#keyObject

Returns a unique key for the server.



7
8
9
# File 'lib/memcache_server.rb', line 7

def key
  "#{host}:#{port}"
end

#socket!Object

Returns the open socket or raises a MemCache::MemCacheError if one is not available.



12
13
14
# File 'lib/memcache_server.rb', line 12

def socket!
  socket or raise MemCacheError, "No connection to server"
end

#stats(type = nil) ⇒ Object

Returns a hash of {stat_key => value, ...} for all of the Server stats.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/memcache_server.rb', line 17

def stats(type = nil)
  unless type.nil? || STAT_TYPES.include?(type)
    raise MemCacheError, "Not a valid stat type.  Valid stat types are: #{STAT_TYPES * ', '}"
  end

  sock = socket!

  stats = {}
  begin
    sock.write "stats#{type ? ' ' + type : nil}\r\n"

    # Loop through the status which are sent back in the format of:
    # STAT <name> <value>\r\n
    # and ends with
    # END\r\n
    while (text = sock.gets)
      break if text =~ /^END/
      
      s, name, value = text.split(/ /)
      stats[name] = value[0..-3]
    end
  rescue SystemCallError, IOError => err
    server.close
    raise MemCacheError, err.message
  end

  stats
end