Memcache的Fragment_exist找不到缓存信息

时间:2012-10-16 09:01:23

标签: ruby-on-rails memcached

我在Heroku上使用Rails,Dalli gem,friendly_id和Memcachier。

我的问题类似于an issue I have had previously但在我开始使用Memcache而不是默认的Rails缓存后停止工作。应该注意的是,我对Rails缓存并不是很熟悉,很可能我做了很多错误(或者没有考虑简单的事情)。

production.rb

  config.action_controller.perform_caching = true
  config.cache_store = :dalli_store, 'mc2.ec2.memcachier.com:11211', { :namespace => 'my_app_name', :expires_in => 40.days, :compress => true, :username => 'asdf', :password => 'asdf' }

礼品#show - controller

unless fragment_exist?("gift-show--" + @gift.slug)
  # Perform slow database fetches etc
end

礼品#show - view

<% cache('gift-show--' + @gift.slug) do %>
 # Create HTML with lots of images and such
<% end %>

在我开始在Heroku上使用Memcachier之前,这个工作正常。我的猜测是fragment_exist?不检查Memcachier,而是检查“默认Rails缓存”(如果存在差异)。我尝试使用Rails.cache.exist?("gift-show--" + @gift.slug)代替fragment_exist?,但它不起作用。

我已经加载了特定的礼物#show-view几次,以确保它被缓存。在日志中我还可以看到Read fragment views/gift-show--cash-stash (1.3ms)(在控制器之后),我相信这是一个片段实际存在的证据。只是当它没有必要时,它会通过缓慢的(4秒)礼物#show-controller。

如果我在Heroku上进入控制台并输入“Rails.cache.read('gift-show--cash-stash')”,我会收到零响应。

另一个特殊的事情是,如果在控制台中执行以下操作:

irb(main):014:0> Rails.cache.write("foo", "bar")
=> true
irb(main):015:0> Rails.cache.read("foo")
=> nil

这很奇怪,不是吗?

那么,我应该使用什么,而不是fragment_exist?为了使这项工作?

1 个答案:

答案 0 :(得分:1)

我不是100%确定这是解决方案,但我添加了'memcachier'宝石(我没有)并将我的production.rb改为:

  config.cache_store = :dalli_store

这实际上也解决了另一个,completely different issue,令我非常惊讶!