Skip to content

Instantly share code, notes, and snippets.

@pixyj
Forked from bkimble/gist:1365005
Last active March 2, 2016 06:53
Show Gist options
  • Save pixyj/49b2ac05dfa7ff485c11 to your computer and use it in GitHub Desktop.
Save pixyj/49b2ac05dfa7ff485c11 to your computer and use it in GitHub Desktop.
List local memcached keys using Ruby
#!/usr/bin/env ruby
require 'net/telnet'
require 'terminal-table'
# This code is copied from a comment on the original repo.
tbl = Terminal::Table.new headings: %w(id expires cache_key bytes)
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
matches = localhost.cmd("String" => "stats items", "Match" => /^END/).scan(/STAT items:(\d+):number (\d+)/)
slabs = matches.inject([]) { |items, item| items << Hash[*['id','items'].zip(item).flatten]; items }
slabs.each do |slab|
localhost.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}", "Match" => /^END/) do |c|
matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data|
cache_key, bytes, expires_time = key_data
tbl << [slab['id'], Time.at(expires_time.to_i), cache_key, bytes]
end
end
end
puts tbl
localhost.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment