n8 / multi_fetch_fragments

Multi-fetch Fragments makes rendering and caching a collection of template partials easier and faster.
http://ninjasandrobots.com/rails-faster-partial-rendering-and-caching
MIT License
539 stars 63 forks source link

Multi fetch slower than no cache because of digest calculation #31

Open pepesenaris opened 9 years ago

pepesenaris commented 9 years ago

I have the following scenario: A partial rendered ( called _folder_results_table.html.erb) every 5 ms in a js view like this. "<%= j(render('folder_results_table')) %>"; and within that partial I'm trying to render and cache a collection of hashes using this gem, like this: <%= render :partial => "result_asset", collection: @somegroups, as: :result, locals: {group_name: group_name, group_name_to_id: group_name_to_id } , cache: Proc.new { |item| [ item[:result][:asset], item[:job][:id], item[:name_collides] ] } %>

The problem is that the digest for the parent cache is calculated in every item of the collection as given by the Rails log

Cache digest for folders/_folder_results_table.html: 44ebc3150678502edd950093fd618343 Cache digest for folders/result_asset.html: 46e854eaa29fb504ccd6786e10617704 ... a 100 items later Cache digest for folders/_folder_results_table.html: 44ebc3150678502edd950093fd618343 Cache digest for folders/result_asset.html: 46e854eaa29fb504ccd6786e10617704.

Why does the digests needs to be calculated in every item? This slowing the process some much that it's faster to not cache the result_asset partial at all. Is there any workaround, or I'm just stuck to not caching the collection which in real production data can have hundreds of elements and will be rendered every 5 sec or so (I know this is bad but right now can not be changed).

Best Regards

travisp commented 9 years ago

@pepesenaris This isn't a multi_fetch_fragments issue, this is just rails caching, and most likely just your default settings for development (in production, digests are not usually recalculated every time).

pepesenaris commented 9 years ago

Thanks for your response.

Hmmm, i see, it makes sense that in production digests are not recalculated.... Do you have any idea of which settings should be set in development to simulate that particular behaviour?

Cheers

coderdave commented 9 years ago

I also had to set config.cache_classes = true in development.rb so that the digests aren't recalculated.

masterkain commented 8 years ago

even with config.cache_classes = true the problem persists for me. it may not have anything to do with the gem but I noticed that once the gem is loaded and partial rendering is set up template digests will be upfront -- without the gem the cache digesting is sparse in logs, so one might not notice at first.

however it's a very strange behavior, my cached partial renders 3/4 more partials and each one needs to be digested at every loop, consuming up to 90% of the request time in my case.

maybe this https://github.com/indirect/haml-rails/pull/51/files#diff-d6edf10e631b76d76eb4f9d9f6908471R41 can help with haml users

ghiculescu commented 8 years ago

I had the same issue, this hotfix works around it: https://github.com/ghiculescu/multi_fetch_fragments/commit/59612d54fc3f9bc38170f9c74b5e753a814f3578