我在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' }
Gift#show - controller
unless fragment_exist?("gift-show--" + @gift.slug)
# Perform slow database fetches etc
end
Gift#show - view
<% cache('gift-show--' + @gift.slug) do %>
# Create HTML with lots of images and such
<% end %>
在我开始在Heroku上使用Memcachier之前,这很好用 . 我的猜测是 fragment_exist?
不会在Memcachier中检查,而是在"default Rails cache"中检查(如果存在差异) . 我试图使用 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 回答
我不是100%确定这是解决方案,但我 added the 'memcachier' gem (我没有)并将我的production.rb改为:
这实际上也解决了另一个,令我惊讶的是!