我正在尝试将片段缓存与 Jbuilder 一起使用,因为其中一个视图需要相当长的时间来渲染(只有视图渲染可能需要 2500 毫秒+
请注意,这在其他调用中确实有效,但这个似乎不起作用,我不知道为什么。第二:这适用于我的本地机器,但在 heroku 中失败。
这是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'
这是我尝试使用片段缓存的简单部分:
json.cache! [category[:products], I18n.locale] do
json.products category[:products] do |product|
json.partial! product
end
end
和产品部分:
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
- 请注意, app_style 部分在任何地方都可以使用,并且可以与其他缓存视图一起使用