Class: MemcachedStats

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", port = 11211) ⇒ MemcachedStats

Returns a new instance of MemcachedStats.



25
26
27
28
29
30
# File 'lib/memcached_stats/base.rb', line 25

def initialize(host = "localhost", port = 11211)
  @host = host
  @port = port.to_i
  @slab_keys = { }
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



142
143
144
145
146
147
148
# File 'lib/memcached_stats/base.rb', line 142

def method_missing(method, *arguments, &block)
  if @summary && @summary.key?(method.to_s)
    @summary[method.to_s]
  else
    super
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



2
3
4
# File 'lib/memcached_stats/base.rb', line 2

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



2
3
4
# File 'lib/memcached_stats/base.rb', line 2

def port
  @port
end

#slab_keysObject (readonly)

Returns the value of attribute slab_keys.



2
3
4
# File 'lib/memcached_stats/base.rb', line 2

def slab_keys
  @slab_keys
end

#slabsObject (readonly)

Returns the value of attribute slabs.



2
3
4
# File 'lib/memcached_stats/base.rb', line 2

def slabs
  @slabs
end

#summaryObject (readonly)

Returns the value of attribute summary.



2
3
4
# File 'lib/memcached_stats/base.rb', line 2

def summary
  @summary
end

Class Method Details

.service_connection(command, host, port, &block) ⇒ Object

Altered code from watsonian/memcache_do gem



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/memcached_stats/base.rb', line 5

def self.service_connection(command, host, port, &block)
  begin
    data = ''
    sock = TCPSocket.new(host, port)
    sock.print("#{command}\r\n")
    sock.flush
    stats = sock.gets
    while true
      data += stats
      break if ['END', 'OK', 'ERROR'].include? stats.strip
      stats = sock.gets
    end
    sock.close
    data
  rescue => e
    ## ToDo: REVISIT LATER, MAYBE RAISE SOMETHING
    #puts e
  end
end

Instance Method Details

#fetch_all_slab_keysObject

# Commented out since I feel (at least at the moment) that it is out of the scope of what

#   this gem should do.
def flush
  MemcachedStats.service_connection("flush_all", @host, @port)
end


48
49
50
51
52
53
54
# File 'lib/memcached_stats/base.rb', line 48

def fetch_all_slab_keys
  get_slabs unless @slabs
  @slabs.each do |slab|
    fetch_single_slab_keys(slab)
  end
  true
end

#fetch_single_slab_keys(slab) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/memcached_stats/base.rb', line 57

def fetch_single_slab_keys(slab)
  slab_id = slab.is_a?(Fixnum) ? slab : (slab.is_a?(Array) ? slab.first : nil)
  
  MemcachedStats.service_connection("stats cachedump #{slab_id} 0", @host, @port).sub("END\r\n","").each_line do |line|
    #tmp = line.sub("ITEM ", "").split(" ").first.split(":")
    
    ## ToDo: Colon might not be delineator
    
    tmp = line.match(/ITEM\ (.*?)\ \[(\d*)\ b\;\ (\d*)\ s\]/).to_a[1..-1] # 0:key, 1:size, 2:expires
    #ITEM User:3293:EmailCount [4 b; 1289245903 s]
    #   [size; expires_timestamp]
    
    record_slab_data(tmp, slab_id)
  end
  true
end

#fetch_statsObject



32
33
34
35
36
# File 'lib/memcached_stats/base.rb', line 32

def fetch_stats
  get_summary
  get_slabs
  self
end