Rails 3高速缓存转储错误

问题描述:

我对应用程序使用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是播放器模型的一个类的方法作为数据库查询的结果返回一个玩家数组。它只是一个普通的旧数组。

什么似乎是错误..我尝试了几种方法来分析这个,但没有工作。请帮忙

缓存使用标准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

你是肯定它是一个数组,而不是一个ActiveRecord关系?我有这个错误,并且只有在我将它转换为数组后才会消失。所以,我的代码

Model.joined_model.where(blah) 

不得不成为

Model.joined_model.where(blah).to_a 

,远离它去!