Skip to content

Instantly share code, notes, and snippets.

@edjames
Last active August 29, 2015 14:01
Show Gist options
  • Save edjames/9d5681d608b302dba855 to your computer and use it in GitHub Desktop.
Save edjames/9d5681d608b302dba855 to your computer and use it in GitHub Desktop.
List all keys in memcache
# .memcached-dump.macdots_function
# mac-dots custom function
# requires mac-dots version >= 0.7.5
# https://gist.github.com/edjames/9d5681d608b302dba855
# Usage:
# $> memcached-dump
memcached-dump() {
_memcached-dump
}
function _memcached-dump {
/usr/bin/env ruby <<-EORUBY
# shamelessly lifted from https://gist.github.com/bkimble/1365005
require 'net/telnet'
headings = %w(id expires bytes cache_key)
rows = []
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] }
longest_key_len = 0
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
rows << [slab['id'], Time.at(expires_time.to_i).strftime('%Y-%m-%d %H:%M:%S'), bytes, cache_key]
longest_key_len = [longest_key_len, cache_key.length].max
end
end
end
row_format = %Q(|%8s | %22s | %8s | %-#{longest_key_len}s |)
puts row_format%headings
rows.each { |row| puts row_format%row }
localhost.close
EORUBY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment