Skip to content

Instantly share code, notes, and snippets.

@michelemina
Created June 18, 2014 15:03
Show Gist options
  • Save michelemina/3c5b61259dd4ade5c453 to your computer and use it in GitHub Desktop.
Save michelemina/3c5b61259dd4ade5c453 to your computer and use it in GitHub Desktop.
Memcached telnet client (forked from another gist)
#!/usr/bin/env ruby
require 'net/telnet'
cache_dump_limit = 100
servers = ["localhost"]
servers.each do |host|
p host
begin
server = Net::Telnet::new("Host" => host, "Port" => 11211, "Timeout" => 3)
slab_ids = []
server.cmd("String" => "stats items", "Match" => /^END/) do |c|
matches = c.scan(/STAT items:(\d+):/)
slab_ids = matches.flatten.uniq
end
puts
puts "Expires At\t\t\t\tCache Key"
puts '-'* 80
slab_ids.each do |slab_id|
server.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
humanized_expires_time = Time.at(expires_time.to_i).to_s
puts "[#{humanized_expires_time}]\t#{cache_key}"
end
end
end
puts
server.close
rescue
p "Error Retrieving"
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment