Skip to content

Instantly share code, notes, and snippets.

@archan937
Last active January 31, 2022 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save archan937/f441146e1aeaa9da138e337514ee4000 to your computer and use it in GitHub Desktop.
Save archan937/f441146e1aeaa9da138e337514ee4000 to your computer and use it in GitHub Desktop.
require "net/telnet"
def keys(hosts, pattern = nil)
[hosts].flatten.each do |host|
slabs = {}
telnet = Net::Telnet::new("Host" => host, "Port" => 11211, "Timeout" => 3)
telnet.cmd("String" => "stats items", "Match" => /^END/) do |stats|
slabs = Hash[stats.scan(/STAT items:(\d+):number (\d+)/)]
end
puts "HOST: #{host}"
slabs.each do |(id, count)|
puts "SLAB: #{id} (#{count})"
telnet.cmd("String" => "stats cachedump #{id} #{count}", "Match" => /^END/) do |stats|
stats.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |item|
key, bytes, expires_time = item
puts key if pattern.nil? || key.match(pattern)
end
end
end
telnet.close
end
nil
end
keys "<your memcached host or hosts>", "<your optional filter pattern>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment