2013-10-28 23 views
1

我一直在跨多個網站共享頁腳緩存問題,我想知道什麼可能會起來。這裏的錯誤消息,並且回溯:「您正在嘗試緩存無法序列化爲memcached的Ruby對象。」

Cache read: remote_footer_information ({:expires_in=>300 seconds}) 
Cache generate: remote_footer_information ({:expires_in=>300 seconds}) 
Cache write: remote_footer_information ({:expires_in=>300 seconds}) 
Marshalling error for key 'remote_footer_information': no _dump_data is defined for class Proc 
You are trying to cache a Ruby object which cannot be serialized to memcached. 

/app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/server.rb:397:in `dump' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/server.rb:397:in `serialize' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/server.rb:269:in `set' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/server.rb:60:in `request' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/options.rb:18:in `block in request' 
    /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/options.rb:17:in `request' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/client.rb:348:in `perform' 
    /app/vendor/bundle/ruby/1.9.1/gems/dalli-2.6.4/lib/dalli/client.rb:199:in `set' 
    (eval):7:in `block in set_with_newrelic_trace' 
    ... 

這裏就是被定義緩存檢索的地方:

require 'httparty' 

module Myclassname 
    class Footer 
    include HTTParty 
    format :json 
    attr_accessor :response 

    def initialize 
     @response = Rails.cache.fetch("remote_footer_information", :expires_in => 5.minutes) do 
     begin 
      self.class.get("http://www.myappname.net/pages/my_footer.json") 
     rescue 
      false 
     end 
     end 
    end 

    def valid? 
     unless @response == false || @response.blank? 
     return @response['footer'] != nil 
     end 

     false 
    end 

    def footer 
     unless @response == false || @response.blank? 
     return @response['footer'] 
     end 

     nil 
    end 

    end 
end 

下面是在這被稱爲視圖的地方。

<%= yield :footer_legend %> 

<div class="clearer"></div> 
<div class="divider"> 
</div> 

<% response = Myclassname::Footer.new %> 
<% if response && response.valid? %> 
    <%= response.footer.html_safe %> 
<% end %> 

我在做什麼錯在這裏?任何想法將不勝感激。謝謝!

+0

爲什麼你在這種情況下不使用片段緩存。 – Muntasim

回答

7

顯然Httparty.get返回的Httparty::Response對象不可序列化,需要進行緩存。嘗試緩存您需要的部分響應,例如

response = self.class.get("http://www.myappname.net/pages/my_footer.json") 
response['footer'] if response 

其實這個問題描述的是here

+0

工作就像一個魅力!感謝您的洞察力! – isthmuses

+0

這是男人的事!非常感謝 – facundofarias

1

將HTTParty響應轉換爲散列,適用於我。

Rails.cache.fetch(cache_key) do 
    HTTParty.get(url).to_hash 
end