Last active
January 31, 2022 19:48
-
-
Save archan937/f441146e1aeaa9da138e337514ee4000 to your computer and use it in GitHub Desktop.
List all Memcached keys using Ruby (inspired by https://gist.github.com/bkimble/1365005/b0a4566f3abdd43dad79bec6542929bf927a0ab3)
This file contains 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
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