Memcache的Fragment_exist找不到缓存的信息 [英] Fragment_exist for Memcache does not find cached info
问题描述
我在Heroku上使用Rails,Dalli gem,friendly_id和Memcachier.
I am using Rails, Dalli gem, friendly_id and Memcachier on Heroku.
我的问题类似于我以前遇到的问题但是在我开始使用Memcache而不是默认的Rails缓存后,该操作停止了.应该注意的是,我对Rails缓存不是很熟悉,很可能我做错了很多事情(或者没有考虑简单的事情).
My problem is similar to an issue I have had previously but that stopped working after I started using Memcache instead of the default Rails cache. It should be noted that I am not very familiar with Rails caching and it is quite likely that I do many things wrong (or fail to consider simple things).
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' }
礼物#显示-控制器
unless fragment_exist?("gift-show--" + @gift.slug)
# Perform slow database fetches etc
end
礼物#显示-查看
<% 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?
,但是它不起作用.
This worked fine before I started using Memcachier on Heroku. My guess is that fragment_exist?
does not check in Memcachier but rather in "default Rails cache" (if there is a difference). I have tried to use Rails.cache.exist?("gift-show--" + @gift.slug)
instead of fragment_exist?
but it doesn't work.
我已经多次加载特定的礼物#show-view,以确保将其缓存.在日志中,我还可以看到Read fragment views/gift-show--cash-stash (1.3ms)
(在控制器之后),我相信这是一个片段实际存在的证明.只是在不需要时,它正在经历缓慢的(4秒)gift#show-controller.
I have loaded the particular gift#show-view a few times to make sure it is cached. In the logs I can also see Read fragment views/gift-show--cash-stash (1.3ms)
(after the controller) which I believe is a proof for that a fragment actually exist. It is just that it is going through the slow (4 seconds) gift#show-controller when it is not necessary.
如果我在Heroku上进入控制台并键入"Rails.cache.read('gift-show--cash-stash')
",则会收到零响应.
If I enter the console on Heroku and type "Rails.cache.read('gift-show--cash-stash')
" I get a nil response.
另一件事是,如果在控制台中执行以下操作:
Another peculiar things is that if do the following in the console:
irb(main):014:0> Rails.cache.write("foo", "bar")
=> true
irb(main):015:0> Rails.cache.read("foo")
=> nil
这很奇怪,不是吗?
那么,我应该用什么代替fragment_exist?为了使这项工作?
So, what should I use, instead of fragment_exist? in order to make this work?
推荐答案
我不是100%确定这是解决方案,但我添加了"memcachier" gem (我没有)并将我的production.rb更改为:
I am not 100% sure this is the solution but I added the 'memcachier' gem (which I didn't have) and altered my production.rb to:
config.cache_store = :dalli_store
这实际上还解决了另一个完全不同的问题,我的大惊喜!
This actually also solved another, completely different issue, to my great surprise!
这篇关于Memcache的Fragment_exist找不到缓存的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!