2011-04-26 93 views
1

将Rails 3与memcachestore和memcache-client gem结合使用。Rails.cache.read后面的Rails.cache.read没有给出正确的值

临时环境中macbook-pro或memcache服务器上的本地memcache。

当我做了

Rails.cache.increment(key, 1) 

随后很快(W /在几行)由

Rails.cache.read(key, :raw => true) 

我让我的原始值。如果它坐了一秒钟,然后我打电话给阅读(或在控制台),我得到正确增加的值。

正如我的工作,我使用增量调用返回的值,但这似乎不应该发生。

任何想法?

+0

听起来像这样的事件驱动的问题。你没有使用EventMachine吗? – Daniel 2011-05-22 20:14:55

回答

0

这个问题在博客中 http://thomasmango.com/2009/06/25/a-better-rails-cache-increment/

描述和建议的解决方法是:

class Util::Cache 
    def self.increment(key, amount = 1) 
    if (value = Rails.cache.read(key)).nil? 
     Rails.cache.write(key, (value = amount)) 
    else 
     Rails.cache.write(key, (value = value + amount)) 
    end 

    return value 
    end 

    def self.decrement(key, amount = 1) 
    if (value = Rails.cache.read(key)).nil? 
     value = 0 
    else 
     Rails.cache.write(key, (value = value - amount)) 
    end 

    return value 
    end 
end