2012-01-13 68 views
8

我有一个rails应用程序,因为我使用简单的rails缓存。我的测试如下:Rails缓存过期

Rails.cache.write('temp',Date.today,:expires_in => 60.seconds) 

我可以通过Rails.cache.read('temp')Rails.cache.fetch('temp')阅读。

问题是它没有过期。 60秒后它仍然活着。任何人都可以告诉我这里缺少什么。

FYI:我在development.rb已经声明如下:

config.action_controller.perform_caching = true 

config.cache_store = :memory_store 

有什么我错过了?我想过期缓存。

回答

9

经过一番搜索,我发现了一个可能的原因,为什么在60秒后缓存没有被清理。

  1. 你打电话Rails.cache.write这是​​记录。
  2. 它调用write_entry(namespaced_key(name, options), entry, options),其中您的选项:expires_inoptions参数的一部分。
  3. implementation of write_entry具有以下条件:

    if expires_in > 0 && !options[:raw] 
        # Set the memcache expire a few minutes in the future to support race condition ttls on read 
        expires_in += 5.minutes 
    end 
    

所以有5分钟加入到你的60秒。 2个可能的解决方案:

  • 它只是生活:-)
  • 尝试包括选择:raw => true,也许这将跳过的条件,让你的工作期满被怀疑。
+0

感谢您的重播....让我试试,让你知道 – palani 2012-01-13 06:59:41

+0

是mliebelt权,也是一个很好的轨道缓存文档,你可以在这里找到http://guides.rubyonrails.org/caching_with_rails.html – 2012-01-13 07:23:46

5

:expires_in选项仅适用于兼容的商店(例如memcached) - 不是内存。

http://guides.rubyonrails.org/caching_with_rails.html

最后,如果你正在使用的memcached或的Ehcache,也可以通过 :expires_in。事实上,caches_action未使用的所有参数都是 发送到底层缓存存储。