Question

I'm trying to use fragment caching with Jbuilder as one of the view is taking quite a bit to render (only view rendering can take 2500ms+

Note that this does work in other calls, but this one can't seem to work and I can't figure why. Second : this works on my local machine but fails in heroku.

Here is the error in heroku :

2013-09-18T21:05:46.425034+00:00 app[web.1]:   Rendered api/shop/products/_product.json.jbuilder (3.2ms) 
2013-09-18T21:05:46.606141+00:00 app[web.1]: Marshalling error for key 'shop/products/344-20130914175034924266000/shop/products/346-20130914175035358419000/shop/products/345-20130914175035153905000/en/b5262bbbd44fb696ffdece67a464e218': no _dump_data is defined for class Proc 
2013-09-18T21:05:46.606141+00:00 app[web.1]: You are trying to cache a Ruby object which cannot be serialized to memcached. 
2013-09-18T21:05:46.606141+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/dalli-2.6.4/lib/dalli/server.rb:397:in `dump' 
2013-09-18T21:05:46.606141+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/dalli-2.6.4/lib/dalli/server.rb:397:in `serialize' 
2013-09-18T21:05:46.606141+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/dalli-2.6.4/lib/dalli/server.rb:269:in `set' 
2013-09-18T21:05:46.606141+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/dalli-2.6.4/lib/dalli/server.rb:60:in `request' 
2013-09-18T21:05:46.606141+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/dalli-2.6.4/lib/dalli/options.rb:18:in `block in request'

Here's the simple part where I tried to use the fragment cache:

json.cache! [category[:products], I18n.locale] do
  json.products category[:products] do |product|
    json.partial! product
  end
end

And the product partial :

json.(
  product,
  :id,
  :name,
  :picture,
  :price,
  :subcategory
)

json.product_options product.product_options do |option|
  json.(
    option,
    :id,
    :name,
    :option_type
  )

  json.option_items option.product_option_items do |item|
    json.(
      item,
      :id,
      :name
    )
  end
end

json.partial! 'api/app_styles/app_style', app_style: product.app_style
  • note that the app_style partial is used everywhere and works with other cached views
Was it helpful?

Solution 2

So after some deep digging, I finally discovered that it is a problem with

  1. rails_stdout_logging gem
  2. paperclip gem
  3. paperclip config with S3 storage
  4. jbuilder

With the combination of those 3 things + using the paperclip object instead of the url (i.e json.node_name my_model.paperclip_attached_file instead of json.node_name my_model.paperclip_attached_file.url) made it fails.

Now seems to work faiirly…

So Matthew were right on target saying that the picture was the problematic one, but only with rails_stdout_gem + paperclip S3 config

OTHER TIPS

The error: You are trying to cache a Ruby object which cannot be serialized to memcached

This is where you should look, its trying to marshal an object with a proc, procs are only evaluated at runtime and cannot be serialized.

If I had to guess it would be your picture attribute, is probably a dynamic method, perhaps write a new method picture_url which doesn't use a proc, you can confirm which attribute is causing problems by removing one by one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top