-
-
Save ghiculescu/7dc61776edfffb45985d291b622cfbad to your computer and use it in GitHub Desktop.
List all Memcached keys using Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# inspired by https://gist.github.com/archan937/f441146e1aeaa9da138e337514ee4000 | |
require "net-telnet" | |
def keys(hosts, port, pattern = nil) | |
[hosts].flatten.each do |host| | |
slabs = {} | |
telnet = Net::Telnet::new("Host" => host, "Port" => port, "Timeout" => 30) | |
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>", 11211, "<your optional filter pattern>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment