Skip to content

Instantly share code, notes, and snippets.

@chaboo
Forked from bkimble/gist:1365005
Created October 17, 2016 14:22
Show Gist options
  • Save chaboo/d5a02da4af8caf0f9de384c407349a2c to your computer and use it in GitHub Desktop.
Save chaboo/d5a02da4af8caf0f9de384c407349a2c to your computer and use it in GitHub Desktop.

Revisions

  1. @bkimble bkimble revised this gist Jul 30, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    #!/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)
  2. @bkimble bkimble revised this gist Jul 30, 2013. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,27 @@
    #!/usr/bin/env ruby
    require 'net/telnet'

    cache_dump_limit = 100
    headings = %w(id expires bytes cache_key)
    rows = []

    localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
    slab_ids = []
    localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
    matches = c.scan(/STAT items:(\d+):/)
    slab_ids = matches.flatten.uniq
    end
    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 }

    puts
    puts "Expires At\t\t\t\tCache Key"
    puts '-'* 80
    slab_ids.each do |slab_id|
    localhost.cmd("String" => "stats cachedump #{slab_id} #{cache_dump_limit}", "Match" => /^END/) do |c|
    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
    humanized_expires_time = Time.at(expires_time.to_i).to_s
    puts "[#{humanized_expires_time}]\t#{cache_key}"
    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
    end
    puts

    row_format = %Q(|%8s | %28s | %12s | %-#{longest_key_len}s |)
    puts row_format%headings
    rows.each{|row| puts row_format%row}

    localhost.close
  3. @bkimble bkimble created this gist Nov 14, 2011.
    27 changes: 27 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/env ruby
    require 'net/telnet'

    cache_dump_limit = 100
    localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
    slab_ids = []
    localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
    matches = c.scan(/STAT items:(\d+):/)
    slab_ids = matches.flatten.uniq
    end


    puts
    puts "Expires At\t\t\t\tCache Key"
    puts '-'* 80
    slab_ids.each do |slab_id|
    localhost.cmd("String" => "stats cachedump #{slab_id} #{cache_dump_limit}", "Match" => /^END/) do |c|
    matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data|
    (cache_key, bytes, expires_time) = key_data
    humanized_expires_time = Time.at(expires_time.to_i).to_s
    puts "[#{humanized_expires_time}]\t#{cache_key}"
    end
    end
    end
    puts

    localhost.close