2011-04-22 65 views
2

我對應用程序使用Rails 3.0.5和Ruby 1.9.2。在我的開發模式中,我配置了緩存開啓。Rails 3高速緩存轉儲錯誤

config.action_controller.perform_caching = true 
    config.cache_store = :file_store, "#{Rails.root.to_s}/tmp/cache" 

而在動作中的一個,我有這行代碼,

@featured_players = Rails.cache.fetch("featured-players") { Player.featured(8) } 

上面一行返回以下錯誤

TypeError (no marshal_dump is defined for class Mutex): 
    activesupport (3.0.5) lib/active_support/cache/file_store.rb:100:in `dump' 
    activesupport (3.0.5) lib/active_support/cache/file_store.rb:100:in `block in write_entry' 
    activesupport (3.0.5) lib/active_support/core_ext/file/atomic.rb:20:in `atomic_write' 
    activesupport (3.0.5) lib/active_support/cache/file_store.rb:100:in `write_entry' 
    activesupport (3.0.5) lib/active_support/cache/strategy/local_cache.rb:135:in `write_entry' 
    activesupport (3.0.5) lib/active_support/cache.rb:364:in `block in write' 
    activesupport (3.0.5) lib/active_support/cache.rb:519:in `instrument' 

featured是播放器模型的一個類的方法作爲數據庫查詢的結果返回一個玩家數組。它只是一個普通的舊數組。

什麼似乎是錯誤..我嘗試了幾種方法來分析這個,但沒有工作。請幫忙

回答

6

緩存使用標準marshalling緩存你的對象。一,你試圖序列化對象都有Mutex的,但你不能序列化的東西是不是位運行狀態的更小:

一些對象不能被傾倒:如果對象被轉儲的包含綁定,過程或方法對象,類IO的實例或單例對象,將引發TypeError。

問題是,有些東西只作爲運行時信息存在,並且它們不能自動重新創建。

你的播放器中有一個線程互斥體,而且元帥沒有辦法自動序列化一個互斥體。你將不得不實現你自己的序列化;有這樣的Marshal文檔中列出的兩種方法:

  • 實施marshal_dumpmarshal_load方法。
  • 執行_dump_load方法。

你可能會想要去marshal_dumpmarshal_load,因爲它們是最簡單的。

+0

感謝您的解釋。所以..我需要創建FeaturedPlayerCached類,並在其中寫入方法marshal_dump和marshal_load .. ?? – Anand 2011-04-22 08:35:49

+0

但我傾倒的對象是一個玩家對象數組=>我只是傾銷一個非常易於清理的數組(如果這就是正確的話) – Anand 2011-04-22 08:43:48

+0

您的Player對象內有一個Mutex。可能是父類,可能是其中一個屬性。你可以在你的Player類中放置'marshal_dump'和'marshal_load'方法。 – 2011-04-22 09:08:16

2

你是肯定它是一個數組,而不是一個ActiveRecord關係?我有這個錯誤,並且只有在我將它轉換爲數組後纔會消失。所以,我的代碼

Model.joined_model.where(blah) 

不得不成爲

Model.joined_model.where(blah).to_a 

,遠離它去!