2012-10-16 51 views
1

我在Heroku上使用Rails,Dalli gem,friendly_id和Memcachier。Memcache的Fragment_exist找不到緩存信息

我的問題與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' } 

禮品展# - 控制器

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?,但它不起作用。

我已經加載了特定的禮物#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%肯定這是解決辦法,但我添加了「memcachier」寶石(我沒有),並改變了我的production.rb到:

config.cache_store = :dalli_store 

這其實也解決了另一個問題,completely different issue,令我非常驚喜!