ruby 读写 memcached

来源:互联网 发布:数据共享geodata 编辑:程序博客网 时间:2024/05/20 02:26
 
require 'memcached'
class Test
  def initialize()
  
    @cache = Memcached.new( ["www.xxx.net:11211" ] )
  end


  def run
 
msg_key = "test"
gettime = get_delivered_times( msg_key ) 
print gettime
 
  end
 
 


    def get_delivered_times( msg_key ) 
      begin
        delive_times = @cache.get msg_key, false 
      rescue => ex 
        if ex.class.to_s == "Memcached::NotFound"
          @cache.set msg_key, "0",   60, false #1分钟内存在
          delive_times = 0 
        end
      end
      delive_times_new = Integer(delive_times) + 1         
      @cache.set msg_key, delive_times_new.to_s,   60, false 
      delive_times
    end
end


Test.new.run
原创粉丝点击