Rails 3 Cache Dump error
I use Rails 3.0.5 with Ruby 1.9.2 for an app. In my development mode, I have configured caching ON.
config.action_controller.perform_caching = true
config.cache_store = :file_store, "#{Rails.root.to_s}/tmp/cache"
And in one of the actions, I have this line of 开发者_运维问答code,
@featured_players = Rails.cache.fetch("featured-players") { Player.featured(8) }
The above line returns the following error
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
is a class method of Player model that returns an array of players as a result of a db query. Its just a plain old array.
What seems to be the error.. I have tried several approaches to analyze this, but none worked. please help
The cache is using standard marshalling to cache your objects. One of the objects that you're trying to serialize has a Mutex in it but you can't serialize something that is little more than a bit of runtime state:
Some objects cannot be dumped: if the objects to be dumped include bindings, procedure or method objects, instances of class IO, or singleton objects, a TypeError will be raised.
The problem is that some things only exist as runtime information and they cannot be recreated automatically.
You have a thread mutex somewhere in your Player and Marshal has no way of automatically serializing a mutex. You're going to have to implement your own serialization; there are two methods of doing this outlined in the Marshal documentation:
- Implement
marshal_dump
andmarshal_load
methods. - Implement
_dump
and_load
methods.
You'll probably want to go with marshal_dump
and marshal_load
as they're the easiest.
Are you sure that it's an array, and not an ActiveRecord relation? I've had this error, and it only went away after I converted it to an array. So, my code
Model.joined_model.where(blah)
had to become
Model.joined_model.where(blah).to_a
and away it went!
精彩评论