Created — forked from bkimble/gist:1365005

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Delete local memcached keys with specific prefix using Ruby

View gist:2929166
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/env ruby
require 'net/telnet'
 
key_prefix = 'www.your:prefix'
cache_dump_limit = 1000
 
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 "Following keys has been deleted:"
puts '-'* 80
puts
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
if cache_key.start_with?(key_prefix)
humanized_expires_time = Time.at(expires_time.to_i).to_s
puts "#{cache_key}"
localhost.cmd("String" => "delete #{cache_key}", "Match" => /^DELETED/) do |c|
puts c
end
end
end
end
end
puts
 
localhost.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.