module Sinatra::MemcachedInspector

Public Instance Methods

memcached_inspect(options) click to toggle source
# File lib/extensions/memcached_inspector.rb, line 3
def memcached_inspect options
  host = options[:host]
  port = options[:port]
  key = options[:key]
  query = options[:query]

  inspect = inspector host, port

  # Filter by key if defined
  if !key.nil?
    inspect = inspect.select{|pair| pair[:key] == key }.first
  end

  # Filter keys by query/regexp
  if !query.nil?
    inspect = inspect.select{|pair| pair[:key] =~ Regexp.new(query, 'i') }
  end

  inspect
end

Private Instance Methods

inspector(host, port) click to toggle source
# File lib/extensions/memcached_inspector.rb, line 25
def inspector host, port
  # Looks horrible, got it from a gist... yet, it works.

  keys = []
  cache_dump_limit = 9_000 # It's over...

  localhost = Net::Telnet::new("Host" => host, "Port" => port, "Timeout" => 3)
  slab_ids = []

  localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
    matches = c.scan(/STAT items:(\d+):/)
    slab_ids = matches.flatten.uniq
  end

  slab_ids.each do |slab_id|
    localhost.cmd("String" => "stats cachedump #{slab_id} #{cache_dump_limit}", "Match" => /^END/) do |c|
      matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data|
        (cache_key, bytes, expires_time) = key_data
        expires_in = expires_time.to_i
        infinite = false
        infinite = true if Time.at(expires_time.to_i) < Time.now

        keys << { key: cache_key, bytes: bytes, expires_in: expires_in, infinite: infinite }
      end
    end
  end

  keys
end