This might be a little verbose but I had to spend some time with the Rails source code to learn how the caching internals work. Writing things down aids my understanding and I figure that sharing some notes on how things work can't hurt. Skip to the end if you're in a hurry.
Up vote 5 down vote favorite 2 share g+ share fb share tw.
I running into an issue with the Rails. Cache methods on 3.1.0. Rc4 (ruby 1.9.2p180 (2011-02-18 revision 30909) x86_64-darwin10).
The code works fine within the same application on 2.3.12 (ruby 1.8.7 (2011-02-18 patchlevel 334) i686-linux, MBARI 0x8770, Ruby Enterprise Edition 2011.03), but started returning an error following the upgrade. I haven't been able to figure out why yet. The error seems to occur when trying to cache objects that have more than one scope on them.
Also, any scopes using lambdas fail regardless of how many scopes. I have hit failures from these patterns: Rails.cache. Fetch("keyname", :expires_in => 1.
Minute) do Model. Scope_with_lambda end Rails.cache. Fetch("keyname", :expires_in => 1.
Minute) do Model.scope. Scope end This is the error that I receive: TypeError: can't dump hash with default proc from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache.
Rb:627:in `dump' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache. Rb:627:in `should_compress?
' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache. Rb:559:in `initialize' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.
Rc4/lib/active_support/cache. Rb:363:in `new' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache.
Rb:363:in `block in write' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache. Rb:520:in `instrument' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.
Rc4/lib/active_support/cache. Rb:362:in `write' from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0. Rc4/lib/active_support/cache.
Rb:299:in `fetch' from (irb):62 from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0. Rc4/lib/rails/commands/console. Rb:45:in `start' from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.
Rc4/lib/rails/commands/console. Rb:8:in `start' from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0. Rc4/lib/rails/commands.
Rb:40:in `' from script/rails:6:in `require' from script/rails:6:in `' I have tried using the :raw => true option as an alternative, but that isn't working because the Rails.cache. Fetch blocks are attempting to cache objects. Any suggestions?
Thanks in advance! Ruby-on-rails ruby ruby-on-rails-3 caching memcached link|improve this question asked Jun 17 '11 at 21:06shedd7901417 74% accept rate.
Try adding the all method in the end of each scope. Model. Scope_with_lambda.
All – Oleander Jun 17 '11 at 23:08 @Oleander - yep, based on the response below, it seems we got lucky with storing the model objects into cache before. We'll recode to cache the data as opposed to the objects. Thanks for the thoughts!
– shedd Jun 18 '11 at 14:50.
This might be a little verbose but I had to spend some time with the Rails source code to learn how the caching internals work. Writing things down aids my understanding and I figure that sharing some notes on how things work can't hurt. Skip to the end if you're in a hurry.
Why It Happens This is the offending method inside ActiveSupport: def should_compress?(value, options) if options:compress && value unless value. Is_a?(Numeric) compress_threshold = options:compress_threshold || DEFAULT_COMPRESS_LIMIT serialized_value = value. Is_a?(String)?
Value : Marshal. Dump(value) return true if serialized_value. Size >= compress_threshold end end false end Note the assignment to serialized_value.
If you poke around inside cache. Rb, you'll see that it uses Marshal to serialize objects to byte strings before they go into the cache and then Marshal again to deserialize objects. The compression issue isn't important here, the important thing is the use of Marshal.
The problem is that: 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. Some things have state (such as OS file descriptors or blocks) that can't be serialized by Marshal. The error you're noting is this: can't dump hash with default proc So someone in your model has an instance variable that is a Hash and that Hash uses a block to supply default values.
The column_methods_hash method uses such a Hash and even caches the Hash inside @dynamic_methods_hash; column_methods_hash will be called (indirectly) by public methods such as respond_to? And method_missing. One of respond_to?
Or method_missing will probably get called on every AR model instance sooner or later and calling either method makes your object unserializable. So, AR model instances are essentially unserializable in Rails 3. Interestingly enough, the respond_to?
And method_missing implementations in 2.3.8 are also backed by a Hash that uses a block for default values. The 2.3.8 cache is "...is meant for caching strings. " so you were getting lucky with a backend that could handle whole objects or it used Marshal before your objects had hash-with-procs in them; or perhaps you were using the MemoryStore cache backend and that's little more than a big Hash.
Using multiple scope-with-lambdas might end up storing Procs in your AR objects; I'd expect the lambdas to be stored with the class (or singleton class) rather than the objects but I didn't bother with an analysis as the problem with respond_to? And method_missing makes the scope issue irrelevant. What You Can Do About It I think you've been storing the wrong things in your cache and getting lucky.
You can either start using the Rails cache properly (i.e. Store simple generated data rather than whole models) or you can implement the marshal_dump/marshal_load or _dump/_load methods as outlined in Marshal. Alternatively, you can use one of the MemoryStore backends and limit yourself to one distinct cache per server process.
Executive Summary You can't depend on storing ActiveRecord model objects in the Rails cache unless you're prepared to handle the marshalling yourself or you want to limit yourself to the MemoryStore cache backends.
Thanks so much for the awesome response! I think your diagnosis is exactly correct - one of the parameters in the scope was pulling a value from a Hash. However, I think your larger point that our implementation of Rails.
Cache should be re-written to avoid storing the model objects directly into the cache. We did get lucky before, but we should use this as an opportunity to clean it up. I very much appreciate the detailed analysis - it was extremely helpful!
– shedd Jun 18 '11 at 14:49 +1 Great analysis mu (as always! ) – Michael Durrant Nov 13 '11 at 13:59.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.