用ruby得到memcached的key:
- #enum_memcache_keys.rb:
- require 'rubygems'
- require 'socket'
- class EnumMemcacheKeys
- MEMCACHE_STATS = ["END", "OK", "DELETE", "ERROR"]
- class << self
- def open(host, port)
- s = TCPSocket.open host, port
- yield s if block_given?
- ensure
- s.close if s
- end
- def items(host = 'localhost', port = '11211')
- i = []
- open(host, port) do |s|
- s.send("stats items\r\n", 0)
- until MEMCACHE_STATS.include?(line = s.gets.strip) do
- i << line
- end
- end
- i
- end
- def keys(host = 'localhost', port = '11211')
- keys = []
- open(host, port) do |s|
- buff = items(host, port)
- items = []
- buff.each { |b| items << b.split(':')[1] }
- keys = []
- items.each do |i|
- s.send("stats cachedump #{i} 0\r\n", 0)
- until MEMCACHE_STATS.include?(line = s.gets.strip) do
- keys << line.split(' ')[1]
- end
- end
- end
- keys
- end
- protected :open
- end
- end
- #use
- p EnumMemcacheKeys.items
- p EnumMemcacheKeys.keys('10.0.0.5', '11211')