Ruby遍历MemCached的key

用ruby得到memcached的key: 

  1. #enum_memcache_keys.rb:  
  2.   
  3. require 'rubygems'  
  4. require 'socket'  
  5.   
  6. class EnumMemcacheKeys  
  7.   
  8.   MEMCACHE_STATS = ["END""OK""DELETE""ERROR"]  
  9.   
  10.   class << self  
  11.     def open(host, port)  
  12.       s = TCPSocket.open host, port  
  13.       yield s if block_given?  
  14.     ensure  
  15.       s.close if s  
  16.     end  
  17.   
  18.     def items(host = 'localhost', port = '11211')  
  19.       i = []  
  20.       open(host, port) do |s|  
  21.         s.send("stats items\r\n", 0)  
  22.   
  23.         until MEMCACHE_STATS.include?(line = s.gets.strip) do  
  24.           i << line  
  25.         end  
  26.       end  
  27.         
  28.       i  
  29.     end  
  30.   
  31.     def keys(host = 'localhost', port = '11211')  
  32.       keys = []  
  33.   
  34.       open(host, port) do |s|  
  35.         buff = items(host, port)  
  36.   
  37.         items = []  
  38.         buff.each { |b| items << b.split(':')[1] }  
  39.   
  40.         keys = []  
  41.         items.each do |i|  
  42.           s.send("stats cachedump #{i} 0\r\n", 0)  
  43.           until MEMCACHE_STATS.include?(line = s.gets.strip) do  
  44.             keys << line.split(' ')[1]  
  45.           end  
  46.         end  
  47.       end  
  48.   
  49.       keys  
  50.     end  
  51.   
  52.     protected :open  
  53.   end  
  54.   
  55. end  
  56.   
  57. #use  
  58. p EnumMemcacheKeys.items  
  59. p EnumMemcacheKeys.keys('10.0.0.5''11211')  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值