-
-
Save bkimble/1365005 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby | |
# List all keys stored in memcache. | |
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place. | |
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]; items } | |
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), bytes, cache_key] | |
longest_key_len = [longest_key_len,cache_key.length].max | |
end | |
end | |
end | |
row_format = %Q(|%8s | %28s | %12s | %-#{longest_key_len}s |) | |
puts row_format%headings | |
rows.each{|row| puts row_format%row} | |
localhost.close |
Unfortunately it does not work when the server has several 100 thousand keys...
excellent, thanks a lot!
Nice 😮 . I have customized this script for dump and restore using dalli
gem.
https://gist.github.com/javanux/dbd005ef50ae4b8cea213af058abf771
I'm fairly certain this does not work on memcachier.
I successfully telneted in to our production servers and tried issues the stats cachedump command and got:
CLIENT_ERROR unsupported stats command
bingo! 👍
Thank you! This help me to solve problem of infinite caching(no expiration).
Hello, @bkimble. I have adjusted your Gist a bit, see: https://gist.github.com/archan937/f441146e1aeaa9da138e337514ee4000
The logic is in a method called keys
and you can pass your memcached host (or a list of hosts) and an optional pattern to filter on keys.
Also, prints the slab IDs with all of its keys.
Unfortunately it does not work when the server has several 100 thousand keys...
Same here. Does not work with 60k keys.
@bkimble thanks for this!
Here's yet another fork of your original gist:
https://gist.github.com/ginjo/aaa4a4395fcdbbad3d29
This revision adds a couple of features for working with cache item rows as ruby objects.
I've included a method that mimics your original formatted printing, plus a method that returns all found cache items as an array of hashes.